Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ill int long long
- #define pb push_back
- #define fi first
- #define se second
- #define ld long double
- using namespace std;
- int l,r,mid,n,m,i,j,mod=1e9+7,p[400006],h[400006],h1[400006];
- string s,t,ans,s1;
- bool check(int l)
- {
- if (l==0)return 1;
- set<int> q;
- int i;
- for (i=l;i<=n;i++)
- {
- int x=(h[i]-h[i-l])/p[i-l+1];
- if (x<0)x+=mod;
- q.insert(x);
- }
- for (i=l;i<=m;i++)
- {
- int x=(h1[i]-h1[i-l])/p[i-l+1];
- if (x<0)x+=mod;
- if (q.find(x)!=q.end())return 1;
- }
- return 0;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
- cin>>s>>t;
- n=s.size();
- m=t.size();
- p[1]=1;
- for (i=2;i<=max(n,m);i++)
- p[i]=(p[i-1]*19)%mod;
- for (i=1;i<=n;i++)
- {
- h[i]+=p[i]*(s[i-1]-'a')+h[i-1];
- h[i]%=mod;
- }
- for (i=1;i<=m;i++)
- {
- h1[i]+=p[i]*(t[i-1]-'a')+h1[i-1];
- h1[i]%=mod;
- }
- l=0;r=min(n,m);
- while (l+1<r)
- {
- mid=(l+r)/2;
- if (check(mid))l=mid;
- else r=mid;
- }
- if (check(r))l=r;
- if (l==0)return 0;
- set<int> q;
- for (i=l;i<=n;i++)
- {
- int x=(h[i]-h[i-l])/p[i-l+1];
- if (x<0)x+=mod;
- q.insert(x);
- }
- for (i=l;i<=m;i++)
- {
- int x=(h1[i]-h1[i-l])/p[i-l+1];
- if (x<0)x+=mod;
- if (q.find(x)!=q.end())
- {
- int h=1;
- s1="";
- for (j=i-l;j<l;j++)
- {
- if (h==1 && (ans.size()>0 && t[j]<ans[j-(i-l)]))h=0;
- if (h==1 && (ans.size()>0 && t[j]>ans[j-(i-l)])){h=2;break;}
- s1+=t[j];
- }
- if (ans=="" || h==0)
- ans=s1;
- }
- }
- cout<<ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement