Advertisement
Malinovsky239

Untitled

Jun 1st, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 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 = 1, cnt = 1, 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.         au[cnt].suf = 1;
  28.  
  29.         int sym = s[i] - 'a';      
  30.        
  31.         int now = end;
  32.         while (now) {          
  33.  
  34.             if (au[now].to[sym]) {
  35.  
  36.                 int nxt = au[now].to[sym];
  37.  
  38.                 if (dp[nxt] == dp[now] + 1) {
  39.                     au[new_end].suf = nxt;
  40.                 }
  41.                 else {
  42.                     cnt++;
  43.                     au[nxt].suf = cnt;
  44.                     au[new_end].suf = cnt;
  45.                     dp[cnt] = dp[now] + 1;
  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.  
  51.                     while (now) {                      
  52.                         if (au[now].to[sym] == nxt) {
  53.                             au[now].to[sym] = cnt;
  54.                             dp[cnt] = max(dp[cnt], dp[now] + 1);                                                       
  55.                         }
  56.                         else
  57.                             break;                     
  58.                         now = au[now].suf;                                         
  59.                     }
  60.                 }              
  61.                 break; 
  62.             }
  63.  
  64.             au[now].to[sym] = new_end;
  65.             dp[new_end] = max(dp[new_end], dp[now] + 1);
  66.             now = au[now].suf;
  67.         }
  68.  
  69.         end = new_end;
  70.     }
  71.  
  72.     for (int i = 1; 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 = 1; i <= cnt; i++)  
  80.         printf("suf[%d] = %d\n", i, au[i].suf);
  81.  
  82.    
  83.    
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement