#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(){
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];
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="";
int F = 0, F2 = 0;
for(int j=0;j<min((int)pref.size(),(int)s[mid].size());j++){
if(s[mid][j] < pref[j]) {
F=1; break;
}
else if(s[mid][j]>pref[j]) {
F2=1; break;
}
}
if(!F2 && (F || (int)s[mid].size() <(int) pref.size())) {
L = mid;
l=mid+1;
}
else r=mid-1;
}
l=1; r=n;
while(l<=r){
int mid=(l+r)/2;
string sb="";
int F = 0, F2 = 0;
for(int j=0;j<min((int)pref.size(),(int)s[mid].size());j++){
sb+=s[mid][j];
if(s[mid][j] < pref[j]) {
F=1; break;
}
else if(s[mid][j]>pref[j]) {
F2=1; break;
}
}
if(F2 ) {
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:62:8: warning: variable 'F' set but not used [-Wunused-but-set-variable]
62 | int F = 0, F2 = 0;
| ^
selling_rna.cpp:83: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]
83 | for(int j=0;j<P[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:87: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]
87 | for(int j=0;j<Q[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:81:7: warning: unused variable 'cur' [-Wunused-variable]
81 | int cur=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8164 KB |
Output is correct |
3 |
Correct |
5 ms |
8144 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
4 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1579 ms |
78332 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
10244 KB |
Output is correct |
2 |
Correct |
50 ms |
9384 KB |
Output is correct |
3 |
Correct |
59 ms |
9804 KB |
Output is correct |
4 |
Correct |
47 ms |
9412 KB |
Output is correct |
5 |
Correct |
45 ms |
9388 KB |
Output is correct |
6 |
Correct |
66 ms |
9616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8164 KB |
Output is correct |
3 |
Correct |
5 ms |
8144 KB |
Output is correct |
4 |
Correct |
4 ms |
8140 KB |
Output is correct |
5 |
Correct |
4 ms |
8140 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
5 ms |
8140 KB |
Output is correct |
8 |
Execution timed out |
1579 ms |
78332 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |