Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ill int long long
  3. #define pb push_back
  4. #define fi first
  5. #define se second
  6. #define ld long double
  7. using namespace std;
  8. int l,r,mid,n,m,i,j,mod=1e9+7,p[400006],h[400006],h1[400006];
  9. string s,t,ans,s1;
  10. bool check(int l)
  11. {
  12. if (l==0)return 1;
  13. set<int> q;
  14. int i;
  15. for (i=l;i<=n;i++)
  16. {
  17. int x=(h[i]-h[i-l])/p[i-l+1];
  18. if (x<0)x+=mod;
  19. q.insert(x);
  20. }
  21. for (i=l;i<=m;i++)
  22. {
  23. int x=(h1[i]-h1[i-l])/p[i-l+1];
  24. if (x<0)x+=mod;
  25. if (q.find(x)!=q.end())return 1;
  26. }
  27. return 0;
  28. }
  29. int main()
  30. {
  31. ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  32. cin>>s>>t;
  33. n=s.size();
  34. m=t.size();
  35. p[1]=1;
  36. for (i=2;i<=max(n,m);i++)
  37. p[i]=(p[i-1]*19)%mod;
  38. for (i=1;i<=n;i++)
  39. {
  40. h[i]+=p[i]*(s[i-1]-'a')+h[i-1];
  41. h[i]%=mod;
  42. }
  43. for (i=1;i<=m;i++)
  44. {
  45. h1[i]+=p[i]*(t[i-1]-'a')+h1[i-1];
  46. h1[i]%=mod;
  47. }
  48. l=0;r=min(n,m);
  49. while (l+1<r)
  50. {
  51. mid=(l+r)/2;
  52. if (check(mid))l=mid;
  53. else r=mid;
  54. }
  55. if (check(r))l=r;
  56. if (l==0)return 0;
  57. set<int> q;
  58. for (i=l;i<=n;i++)
  59. {
  60. int x=(h[i]-h[i-l])/p[i-l+1];
  61. if (x<0)x+=mod;
  62. q.insert(x);
  63. }
  64. for (i=l;i<=m;i++)
  65. {
  66. int x=(h1[i]-h1[i-l])/p[i-l+1];
  67. if (x<0)x+=mod;
  68. if (q.find(x)!=q.end())
  69. {
  70. int h=1;
  71. s1="";
  72. for (j=i-l;j<l;j++)
  73. {
  74. if (h==1 && (ans.size()>0 && t[j]<ans[j-(i-l)]))h=0;
  75. if (h==1 && (ans.size()>0 && t[j]>ans[j-(i-l)])){h=2;break;}
  76. s1+=t[j];
  77. }
  78. if (ans=="" || h==0)
  79. ans=s1;
  80. }
  81. }
  82. cout<<ans;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement