Advertisement
Malinovsky239

Untitled

May 28th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int N = int(1e5 + 5);
  9.  
  10. struct node {
  11.     int suf;
  12.     int to[26];    
  13. };
  14.  
  15. node au[N];
  16. int end, cnt, new_end, dp[N];
  17. char s[N];
  18.  
  19. int main() {
  20.     gets(s);
  21.  
  22.     int l = strlen(s);
  23.  
  24.     for (int i = 0; i < l; i++) {
  25.         cnt++;
  26.         new_end = cnt;
  27.  
  28.         int sym = s[i] - 'a';
  29.        
  30.         au[end].to[sym] = new_end;
  31.         dp[new_end] = dp[end] + 1;
  32.  
  33.         int now = end;
  34.         while (now) {
  35.             now = au[now].suf;
  36.  
  37.             if (au[now].to[sym]) {
  38.  
  39.                 int nxt = au[now].to[sym];
  40.                 if (dp[nxt] == dp[now] + 1) {
  41.                     au[new_end].suf = nxt;
  42.                 }
  43.                 else {
  44.                     cnt++;
  45.                     au[nxt].suf = cnt;                 
  46.                    
  47.                     for (int j = 0; j < 26; j++)
  48.                         if (au[nxt].to[j]) {
  49.                             au[cnt].to[j] = au[nxt].to[j];
  50.                             dp[ au[cnt].to[j] ] = max(dp[ au[cnt].to[j] ], dp[j] + 1);
  51.                         }
  52.  
  53.                     while (now) {
  54.                         now = au[now].suf;
  55.                         if (au[now].to[sym] == nxt) {
  56.                             au[now].to[sym] = cnt;
  57.                             dp[cnt] = max(dp[cnt], dp[now] + 1);
  58.                         }                                                  
  59.                     }
  60.                 }
  61.  
  62.                 break; 
  63.             }
  64.             else {
  65.                 au[now].to[sym] = new_end;
  66.                 dp[new_end] = max(dp[new_end], dp[now] + 1);
  67.             }
  68.         }
  69.         end = new_end;
  70.  
  71.  
  72.         for (int i = 0; i <= cnt; i++) {
  73.             for (int j = 0; j < 26; j++)
  74.                 if (au[i].to[j]) {
  75.                     printf("edge %d-%d through %c\n", i, au[i].to[j], 'a' + j);
  76.                 }
  77.         }  
  78.  
  79.         for (int i = 0; i <= cnt; i++)  
  80.             printf("suf[%d] = %d\n", i, au[i].suf);
  81.         puts("----------------");
  82.     }
  83.    
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement