#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=1e5+5,mod=1e9+7;
int t,n,m,pwr[N],ans[N];
vector<pair<int,pair<int,int> > >Q[N],P[N];
map<int,int> cnt;
string s[N];
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;
}
main(){
// t=1;
cin >> n >> m;
int mx = 0;
for(int i=1;i<=n;i++){
cin >> s[i];
mx=max(mx,(int)s[i].size());
}
int p = 37;
pwr[0] = 1;
for(int i=1;i<=mx;i++){
pwr[i]=pwr[i-1]*p%mod;
}
sort(s+1,s+n+1);
string pref,suff;
for(int i=1;i<=m;i++){
cin>>pref>>suff;
int l=1,r=n,L=0,R=n+1;
while(l<=r){
int mid=(l+r)/2;
string sb="";
for(int j=0;j<min((int)pref.size(),(int)s[mid].size());j++){
sb+=s[mid][j];
}
if(sb < pref ) {
L = mid;
l=mid+1;
}
else r=mid-1;
}
l=1; r=n;
while(l<=r){
int mid=(l+r)/2;
string sb="";
for(int j=0;j<min((int)pref.size(),(int)s[mid].size());j++){
sb+=s[mid][j];
}
if(sb > pref ) {
R = mid;
r=mid-1;
}
else l=mid+1;
}
P[L+1].push_back({R-1,{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:13:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
selling_rna.cpp: At global scope:
selling_rna.cpp:19:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
19 | main(){
| ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:69: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]
69 | for(int j=0;j<P[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:73: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]
73 | for(int j=0;j<Q[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:67:7: warning: unused variable 'cur' [-Wunused-variable]
67 | int cur=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
6 ms |
8140 KB |
Output is correct |
4 |
Correct |
6 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
6 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1571 ms |
63348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
10240 KB |
Output is correct |
2 |
Correct |
88 ms |
9380 KB |
Output is correct |
3 |
Correct |
100 ms |
9752 KB |
Output is correct |
4 |
Correct |
79 ms |
9348 KB |
Output is correct |
5 |
Correct |
67 ms |
9400 KB |
Output is correct |
6 |
Correct |
91 ms |
9632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
6 ms |
8140 KB |
Output is correct |
3 |
Correct |
6 ms |
8140 KB |
Output is correct |
4 |
Correct |
6 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
6 ms |
8028 KB |
Output is correct |
8 |
Execution timed out |
1571 ms |
63348 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |