답안 #398604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
398604 2021-05-04T15:23:35 Z keta_tsimakuridze Selling RNA Strands (JOI16_selling_rna) C++14
0 / 100
1500 ms 81696 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define ends endss
using namespace std;
const int N=1e5+5,mod=1e9+7;
int t,n,m,pwr[N],ans[N],tree[N],aft[20*N][5],cur,timer,tmin[20*N],tmout[20*N],L,R,ends[N];
vector<pair<int,pair<int,int> > >Q[N],P[N];
string s[N],s1[N];
map<int,int> cnt;
set<pair<int,int> > S;
vector<pair<int,int> > a;
int gethash(string s,int f){
	reverse(s.begin(),s.end());
	int cur = 0;
	for(int i=0;i<s.size();i++){
		cur=cur+pwr[i]*(s[i]-'a'+1); cur%=mod;
		if(f) cnt[cur]++;
	}
	return cur;
}
int get(char c){
	if(c=='A') return 1;
	if(c=='G') return 2;
	if(c=='U') return 3;
	return 0;
}
void add(string s){
	int u = 0;
	for(int i=0;i<s.size();i++){
		if(!aft[u][get(s[i])]) cur++,aft[u][get(s[i])]=cur;
		
		u=aft[u][get(s[i])];
	}
}
void dfs(int u){
	timer++;
	tmin[u] = timer;
	for(int i=0;i<4;i++){
		if(aft[u][i]) dfs(aft[u][i]);
	}
	tmout[u]=timer;
}
void find(string s){
	int u = 0;
	for(int i=0;i<s.size();i++){
		if(!aft[u][get(s[i])]) return;
		u=aft[u][get(s[i])];
	}	
	if(S.upper_bound({tmin[u],0})!=S.end()) L= (*S.upper_bound({tmin[u],0})).s;
	if(S.upper_bound({tmout[u],n+1})!=S.begin()) R = (*--S.upper_bound({tmout[u],n+1})).s;
//	cout<<u<<" "<<tmout[u]<<" "<<tmin[u]<<" "<<L<<"bbbbbb"<<R<<endl;
}
 main(){
 	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	// t=1;
	cin >> n >> m;
	int mx = 0;
	for(int i=1;i<=n;i++){
		cin >> s[i];
		add(s[i]); ends[i]=cur; 
		mx=max(mx,(int)s[i].size());
	}
	dfs(0);
	int p = 37;
	pwr[0] = 1;
	for(int i=1;i<=mx;i++){
		pwr[i]=pwr[i-1]*p%mod;
	}
	for(int i=1;i<=n;i++){ 
		s1[i] = s[i];;
		a.push_back({tmin[ends[i]],i});
	}
	sort(a.begin(),a.end());
	for(int i=1;i<=n;i++){
		s[i]=s1[a[i-1].s];
		S.insert({a[i-1].f,i}); 
	}
	string pref,suff;
	for(int i=1;i<=m;i++){
	
		cin>>pref>>suff;
		L=n+1; R=0;
		find(pref);
		
		if(L<=R)P[L].push_back({R,{i,gethash(suff,0)}});
	}
	for(int i=1;i<=n;i++){
		int cur=0;
		
		for(int j=0;j<P[i].size();j++){
			Q[P[i][j].f].push_back({cnt[P[i][j].s.s],P[i][j].s});
		}
		gethash(s[i],1);
		for(int j=0;j<Q[i].size();j++){
			ans[Q[i][j].s.f] =cnt[Q[i][j].s.s]-Q[i][j].f; 
		}
	}
	for(int i=1;i<=m;i++){
		cout<<ans[i]<<" ";
	}
}

Compilation message

selling_rna.cpp: In function 'int gethash(std::string, int)':
selling_rna.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0;i<s.size();i++){
      |              ~^~~~~~~~~
selling_rna.cpp: In function 'void add(std::string)':
selling_rna.cpp:30:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  for(int i=0;i<s.size();i++){
      |              ~^~~~~~~~~
selling_rna.cpp: In function 'void find(std::string)':
selling_rna.cpp:46:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=0;i<s.size();i++){
      |              ~^~~~~~~~~
selling_rna.cpp: At global scope:
selling_rna.cpp:54:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 |  main(){
      |       ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:91:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |   for(int j=0;j<P[i].size();j++){
      |               ~^~~~~~~~~~~~
selling_rna.cpp:95:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |   for(int j=0;j<Q[i].size();j++){
      |               ~^~~~~~~~~~~~
selling_rna.cpp:89:7: warning: unused variable 'cur' [-Wunused-variable]
   89 |   int cur=0;
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 11212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1566 ms 81696 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 74 ms 16428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 11212 KB Output isn't correct
2 Halted 0 ms 0 KB -