#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],cur,all;
vector<pair<int,pair<int,int> > >Q[N],P[N];
map<int,int> cnt;
vector<int>ind[N];
string s[N],s1[N];
vector<pair<int,pair<int,int> > > a;
vector<pair<pair<int,int>,pair<int,int> > > c[N];
pair<pair<int,int>,pair<int,int> > p[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;
}
void CountSort(){
for(int i=0;i<=all;i++){
c[i].clear();
}
for(int i=1;i<=all;i++){
c[p[i].f.s].push_back(p[i]);
}
int cur=0;
for(int i=0;i<=all;i++){
for(int j=0;j<c[i].size();j++){
cur++;
p[cur]=c[i][j];
}
}
//
for(int i=0;i<=all;i++){
c[i].clear();
}
for(int i=1;i<=all;i++){
c[p[i].f.f].push_back(p[i]);
}
cur=0;
for(int i=0;i<=all;i++){
for(int j=0;j<c[i].size();j++){
cur++;
p[cur]=c[i][j];
}
}
}
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];
ind[i].resize(s[i].size());
for(int j=0;j<s[i].size();j++){
a.push_back({s[i][j]-'a',{i,j}});
}
mx=max(mx,(int)s[i].size());
all+=(int)s[i].size();
}
int pr = 37;
pwr[0] = 1;
for(int i=1;i<=mx;i++){
pwr[i]=pwr[i-1]*pr%mod;
}
// sort(s+1,s+n+1); ??
sort(a.begin(),a.end());
int cur = 0;
for(int i=0;i<a.size();i++){
if(!i || a[i].f!=a[i-1].f) cur++;
ind[a[i].s.f][a[i].s.s] = cur;
}
int Lg=log2(mx) + 1;
for(int i=1;i<=Lg;i++){cur = 0;
for(int j=1;j<=n;j++){
for(int k=0;k<s[j].size();k++){
int ind2=0;
cur++;
if(k+(1<<(i-1)) < s[j].size()) ind2=ind[j][k+(1<<(i-1))];
p[cur]={{ind[j][k],ind2},{j,k}};
}
}
CountSort();
cur = 0;
for(int j=1;j<=all;j++){
if(p[j].f.f!=p[j-1].f.f || p[j].f.s!=p[j-1].f.s) cur++;
ind[p[j].s.f][p[j].s.s]=cur;
}
}
a.clear();
for(int i=1;i<=n;i++) {
a.push_back({ind[i][0],{i,0}});
s1[i]=s[i];
}
sort(a.begin(),a.end());
cur=0;
for(int i=0;i<n;i++){
s[i+1] = s1[a[i].s.f];
}
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:17:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0;i<s.size();i++){
| ~^~~~~~~~~
selling_rna.cpp: In function 'void CountSort()':
selling_rna.cpp:33:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int j=0;j<c[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:48:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int j=0;j<c[i].size();j++){
| ~^~~~~~~~~~~~
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:62:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int j=0;j<s[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:76:15: 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]
76 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
selling_rna.cpp:83:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int k=0;k<s[j].size();k++){
| ~^~~~~~~~~~~~
selling_rna.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | if(k+(1<<(i-1)) < s[j].size()) ind2=ind[j][k+(1<<(i-1))];
| ~~~~~~~~~~~~~^~~~~~~~~~~~~
selling_rna.cpp:140:8: warning: variable 'F' set but not used [-Wunused-but-set-variable]
140 | int F = 0, F2 = 0;
| ^
selling_rna.cpp:161: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]
161 | for(int j=0;j<P[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:165: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]
165 | for(int j=0;j<Q[i].size();j++){
| ~^~~~~~~~~~~~
selling_rna.cpp:159:7: warning: unused variable 'cur' [-Wunused-variable]
159 | int cur=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16044 KB |
Output is correct |
2 |
Correct |
10 ms |
15928 KB |
Output is correct |
3 |
Correct |
10 ms |
16016 KB |
Output is correct |
4 |
Correct |
11 ms |
15948 KB |
Output is correct |
5 |
Correct |
12 ms |
15948 KB |
Output is correct |
6 |
Correct |
10 ms |
15992 KB |
Output is correct |
7 |
Correct |
10 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
418 ms |
106920 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
27412 KB |
Output is correct |
2 |
Correct |
112 ms |
31108 KB |
Output is correct |
3 |
Correct |
106 ms |
30484 KB |
Output is correct |
4 |
Correct |
99 ms |
25712 KB |
Output is correct |
5 |
Correct |
98 ms |
29964 KB |
Output is correct |
6 |
Correct |
101 ms |
30172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16044 KB |
Output is correct |
2 |
Correct |
10 ms |
15928 KB |
Output is correct |
3 |
Correct |
10 ms |
16016 KB |
Output is correct |
4 |
Correct |
11 ms |
15948 KB |
Output is correct |
5 |
Correct |
12 ms |
15948 KB |
Output is correct |
6 |
Correct |
10 ms |
15992 KB |
Output is correct |
7 |
Correct |
10 ms |
15948 KB |
Output is correct |
8 |
Runtime error |
418 ms |
106920 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |