Rkhamim

Untitled

Apr 10th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define M1 1000000007
  4. #define M 2038074743
  5. long long int a[100005],b[100005],c[100005],d[100005],e[100005],f[100005],g[100005];
  6. struct hash_pair {
  7. template <class T1, class T2>
  8. size_t operator()(const pair<T1, T2>& p) const
  9. {
  10. auto hash1 = hash<T1>{}(p.first);
  11. auto hash2 = hash<T2>{}(p.second);
  12. return hash1 ^ hash2;
  13. }
  14. };
  15. int main()
  16. {
  17. ios_base::sync_with_stdio(false);
  18. cin.tie(NULL);
  19. long long int n,m,x=0,y=0,z,k=0,i,j=0,l,ans=0,q=1,r=1,t=1;
  20. unordered_map< pair<int,int> ,pair<int,int>,hash_pair>ma;
  21. vector<int>v;
  22. cin>>n>>m;
  23. string s,p;
  24. cin>>s;
  25. for(i=0;i<n;i++)
  26. {
  27. x=(x*401+s[i])%M;
  28. y=(y*409+s[i])%M1;
  29. e[i]=y;
  30. a[i]=x;
  31. if(i==0)d[i]=1,f[i]=1;
  32. else d[i]=(d[i-1]*401)%M,f[i]=(f[i-1]*409)%M1;
  33. }
  34. for(i=0;i<m;i++)
  35. {
  36. cin>>p;
  37. y=p.size();
  38. if(c[y]==0)
  39. {
  40. v.push_back(y);
  41. c[y]=1;
  42. }
  43. x=0,y=0;
  44. for(j=0;j<p.size();j++)
  45. {
  46. x=(x*401+p[j])%M;
  47. y=(y*409+p[j])%M1;
  48. }
  49. b[i]=x;
  50. g[i]=y;
  51. //cout<<x<<" ";
  52. ma[{x,y}].first=1;
  53. }
  54. for(i=0;i<v.size();i++)
  55. {
  56. z=v[i];
  57. // cout<<z<<" ";
  58. x=a[z-1];
  59. y=e[z-1];
  60. if(ma[{x,y}].first)
  61. {
  62. ma[{x,y}].first++,ma[{x,y}].second=1;
  63. }
  64. for(j=z;j<n;j++)
  65. {
  66. x=(x-s[j-z]*d[z-1])%M;
  67. x=(x+M)%M;
  68. x=(x*401+s[j])%M;
  69. y=(y-s[j-z]*f[z-1])%M1;
  70. y=(y+M1)%M1;
  71. y=(y*409+s[j])%M1;
  72. if(ma[{x,y}].first&&(ma[{x,y}].second==0||ma[{x,y}].second+z<=j-z+2))
  73. {
  74. //cout<<ma[x].second<<" "<<j<<endl;
  75. ma[{x,y}].first++,ma[{x,y}].second=j-z+2;
  76. }
  77. }
  78.  
  79. }
  80. for(i=0;i<m;i++)
  81. cout<<ma[{b[i],g[i]}].first-1<<endl;
  82. return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment