Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int p[2000008],c[2000007],cnt[2000008],lcp[2000008],ran[2000008],pn[2000007],cn[2000008];
- void sort_cyclic_shifts(string const& s) {
- int n = s.size();
- const int alphabet = 256;
- for (int i = 0; i < n; i++)
- cnt[s[i]]++;
- for (int i = 1; i < alphabet; i++)
- cnt[i] += cnt[i-1];
- for (int i = 0; i < n; i++)
- p[--cnt[s[i]]] = i;
- c[p[0]] = 0;
- int classes = 1;
- for (int i = 1; i < n; i++) {
- if (s[p[i]] != s[p[i-1]])
- classes++;
- c[p[i]] = classes - 1;
- }
- for (int h = 0; (1 << h) < n; ++h) {
- for (int i = 0; i < n; i++) {
- pn[i] = p[i] - (1 << h);
- if (pn[i] < 0)
- pn[i] += n;
- }
- fill(cnt, cnt + classes, 0);
- for (int i = 0; i < n; i++)
- cnt[c[pn[i]]]++;
- for (int i = 1; i < classes; i++)
- cnt[i] += cnt[i-1];
- for (int i = n-1; i >= 0; i--)
- p[--cnt[c[pn[i]]]] = pn[i];
- cn[p[0]] = 0;
- classes = 1;
- for (int i = 1; i < n; i++) {
- pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};
- pair<int, int> prev = {c[p[i-1]], c[(p[i-1] + (1 << h)) % n]};
- if (cur != prev)
- ++classes;
- cn[p[i]] = classes - 1;
- }
- for (int i = 0; i < n; i++)c[i]=cn[i];
- }
- }
- void suffix_array_construction(string s) {
- s += "$";
- sort_cyclic_shifts(s);
- }
- //longest common prefix of string suffix
- void lcp_construction(string const& s) {
- int n = s.size();
- for (int i = 0; i < n; i++)
- ran[p[i]] = i;
- int k = 0;
- for (int i = 0; i < n; i++) {
- if (ran[i] == n - 1) {
- k = 0;
- continue;
- }
- int j = p[ran[i] + 1];
- while (i + k < n && j + k < n && s[i+k] == s[j+k])
- k++;
- lcp[ran[i]] = k;
- if (k)
- k--;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int i,j,k,t,x,y,z;
- string s,s1;
- cin>>s>>s1;
- z=s.size();
- s=s+'#'+s1;
- suffix_array_construction(s);
- lcp_construction(s);
- x=0;
- for(i=0;i<s.size()-1;i++)
- {
- if((p[i]<z&&p[i+1]>z)||(p[i]>z&&p[i+1]<z))
- x=max(x,lcp[i]);
- }
- cout<<x<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment