#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=2e5+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 'long long int gethash(std::string, long long int)':
selling_rna.cpp:14:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
selling_rna.cpp: At global scope:
selling_rna.cpp:20:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main(){
| ^
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:70:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int j=0;j<P[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:74:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int j=0;j<Q[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:68:7: warning: unused variable 'cur' [-Wunused-variable]
68 | int cur=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15948 KB |
Output is correct |
2 |
Correct |
10 ms |
16008 KB |
Output is correct |
3 |
Correct |
10 ms |
15948 KB |
Output is correct |
4 |
Correct |
10 ms |
15948 KB |
Output is correct |
5 |
Correct |
9 ms |
15948 KB |
Output is correct |
6 |
Correct |
10 ms |
15920 KB |
Output is correct |
7 |
Correct |
10 ms |
15888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1565 ms |
86868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
20304 KB |
Output is correct |
2 |
Correct |
88 ms |
18508 KB |
Output is correct |
3 |
Correct |
99 ms |
19164 KB |
Output is correct |
4 |
Correct |
93 ms |
18604 KB |
Output is correct |
5 |
Correct |
70 ms |
18608 KB |
Output is correct |
6 |
Correct |
90 ms |
19284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15948 KB |
Output is correct |
2 |
Correct |
10 ms |
16008 KB |
Output is correct |
3 |
Correct |
10 ms |
15948 KB |
Output is correct |
4 |
Correct |
10 ms |
15948 KB |
Output is correct |
5 |
Correct |
9 ms |
15948 KB |
Output is correct |
6 |
Correct |
10 ms |
15920 KB |
Output is correct |
7 |
Correct |
10 ms |
15888 KB |
Output is correct |
8 |
Execution timed out |
1565 ms |
86868 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |