#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 ind){
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])];
}
ends[ind] = u;
}
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],i);
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, int)':
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:47:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
selling_rna.cpp: At global scope:
selling_rna.cpp:55:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
55 | main(){
| ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:92: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]
92 | for(int j=0;j<P[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:96: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]
96 | for(int j=0;j<Q[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:90:7: warning: unused variable 'cur' [-Wunused-variable]
90 | int cur=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11212 KB |
Output is correct |
2 |
Correct |
8 ms |
11212 KB |
Output is correct |
3 |
Correct |
8 ms |
11248 KB |
Output is correct |
4 |
Correct |
8 ms |
11304 KB |
Output is correct |
5 |
Correct |
8 ms |
11284 KB |
Output is correct |
6 |
Correct |
9 ms |
11216 KB |
Output is correct |
7 |
Correct |
8 ms |
11284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1579 ms |
85020 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
16588 KB |
Output is correct |
2 |
Correct |
44 ms |
14624 KB |
Output is correct |
3 |
Correct |
55 ms |
15748 KB |
Output is correct |
4 |
Correct |
42 ms |
14908 KB |
Output is correct |
5 |
Correct |
50 ms |
14820 KB |
Output is correct |
6 |
Correct |
60 ms |
15684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11212 KB |
Output is correct |
2 |
Correct |
8 ms |
11212 KB |
Output is correct |
3 |
Correct |
8 ms |
11248 KB |
Output is correct |
4 |
Correct |
8 ms |
11304 KB |
Output is correct |
5 |
Correct |
8 ms |
11284 KB |
Output is correct |
6 |
Correct |
9 ms |
11216 KB |
Output is correct |
7 |
Correct |
8 ms |
11284 KB |
Output is correct |
8 |
Execution timed out |
1579 ms |
85020 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |