Kaidul

ALi

Mar 16th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. const int maxN = 250500;
  9. const int maxState = maxN << 1;
  10.  
  11. struct State {
  12.     State *go[26], *suffix;
  13.     int depth, id;
  14.     long long cnt;
  15. };
  16. State pool[maxState], *point, *root, *sink;
  17. int size;
  18.  
  19. State *newState(int dep) {
  20.     point->id = size++;
  21.     point->depth = dep;
  22.     return point++;
  23. }
  24.  
  25. void init() {
  26.     point = pool;
  27.     size = 0;
  28.     root = sink = newState(0);
  29. }
  30.  
  31. void insert(int a) {
  32.     State *p = newState(sink->depth+1);
  33.     State *cur = sink, *sufState;
  34.     while (cur && !cur->go[a]) {
  35.         cur->go[a] = p;
  36.         cur = cur->suffix;
  37.     }
  38.     if (!cur)
  39.         sufState = root;
  40.     else {
  41.         State *q = cur->go[a];
  42.         if (q->depth == cur->depth + 1)
  43.             sufState = q;
  44.         else {
  45.             State *r = newState(cur->depth+1);
  46.             memcpy(r->go, q->go, sizeof(q->go));
  47.             r->suffix = q->suffix;
  48.             q->suffix = r;
  49.             sufState = r;
  50.             while (cur && cur->go[a] == q) {
  51.                 cur->go[a] = r;
  52.                 cur = cur->suffix;
  53.             }
  54.         }
  55.     }
  56.     p->suffix = sufState;
  57.     sink = p;
  58. }
  59. int indx;
  60. int work(string buf) {
  61.     //printf("%s", buf);
  62.     int len = buf.length();
  63.     int tmp = 0, ans = 0;
  64.     State *cur = root;
  65.     for (int i = 0; i < len; i++) {
  66.         if (cur->go[buf[i]-'a']) {
  67.             tmp++;
  68.             cur = cur->go[buf[i]-'a'];
  69.         } else {
  70.             while (cur && !cur->go[buf[i]-'a'])
  71.                 cur = cur->suffix;
  72.  
  73.             if (!cur) {
  74.                 cur = root;
  75.                 tmp = 0;
  76.             } else {
  77.                 tmp = cur->depth + 1;
  78.                 cur = cur->go[buf[i]-'a'];
  79.             }
  80.         }
  81. //        ans = max(ans, tmp);
  82.         if(tmp > ans) {
  83.             ans = tmp;
  84.             indx = i - tmp + 1;
  85.         }
  86.     }
  87.     return ans;
  88. }
  89.  
  90. char ch[maxN];
  91.  
  92. int main() {
  93. //    freopen("in.txt", "r", stdin);
  94.     string S1, S2;
  95.     cin >> S1 >> S2;
  96.     init();
  97.     int len = S1.length();
  98.     for (int i = 0; i < len; i++)
  99.         insert(S1[i]-'a');
  100.  
  101.     len = work(S2);
  102.     if(len > 0) cout << S2.substr(indx, len) << endl;
  103.     printf("%d\n", len);
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment