Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define M1 1000000007
- #define M 2038074743
- long long int a[100005],b[100005],c[100005],d[100005],e[100005],f[100005],g[100005];
- struct hash_pair {
- template <class T1, class T2>
- size_t operator()(const pair<T1, T2>& p) const
- {
- auto hash1 = hash<T1>{}(p.first);
- auto hash2 = hash<T2>{}(p.second);
- return hash1 ^ hash2;
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- long long int n,m,x=0,y=0,z,k=0,i,j=0,l,ans=0,q=1,r=1,t=1;
- unordered_map< pair<int,int> ,pair<int,int>,hash_pair>ma;
- vector<int>v;
- cin>>n>>m;
- string s,p;
- cin>>s;
- for(i=0;i<n;i++)
- {
- x=(x*401+s[i])%M;
- y=(y*409+s[i])%M1;
- e[i]=y;
- a[i]=x;
- if(i==0)d[i]=1,f[i]=1;
- else d[i]=(d[i-1]*401)%M,f[i]=(f[i-1]*409)%M1;
- }
- for(i=0;i<m;i++)
- {
- cin>>p;
- y=p.size();
- if(c[y]==0)
- {
- v.push_back(y);
- c[y]=1;
- }
- x=0,y=0;
- for(j=0;j<p.size();j++)
- {
- x=(x*401+p[j])%M;
- y=(y*409+p[j])%M1;
- }
- b[i]=x;
- g[i]=y;
- //cout<<x<<" ";
- ma[{x,y}].first=1;
- }
- for(i=0;i<v.size();i++)
- {
- z=v[i];
- // cout<<z<<" ";
- x=a[z-1];
- y=e[z-1];
- if(ma[{x,y}].first)
- {
- ma[{x,y}].first++,ma[{x,y}].second=1;
- }
- for(j=z;j<n;j++)
- {
- x=(x-s[j-z]*d[z-1])%M;
- x=(x+M)%M;
- x=(x*401+s[j])%M;
- y=(y-s[j-z]*f[z-1])%M1;
- y=(y+M1)%M1;
- y=(y*409+s[j])%M1;
- if(ma[{x,y}].first&&(ma[{x,y}].second==0||ma[{x,y}].second+z<=j-z+2))
- {
- //cout<<ma[x].second<<" "<<j<<endl;
- ma[{x,y}].first++,ma[{x,y}].second=j-z+2;
- }
- }
- }
- for(i=0;i<m;i++)
- cout<<ma[{b[i],g[i]}].first-1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment