_no0B

Untitled

Mar 2nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. /// palindromic tree
  2. int nxt[N][26], sfx[N], len[N];
  3. void init(int n){
  4.     memset(nxt[n],-1,sizeof nxt[n]);
  5. }
  6. int palindromic_tree(char s[]){
  7.     int cur = 0, tot = 2;
  8.     len[0] = -1;
  9.     len[1] = sfx[1] = sfx[0] = 0;
  10.     init(0);
  11.     init(1);
  12.     for(int i = 0; s[i]!='\0'; i++){
  13.         while(cur!=0 && s[i-1-len[cur]]!=s[i]) cur = sfx[cur];
  14.         if(nxt[cur][s[i]-'a']!=-1){
  15.             cur = nxt[cur][s[i]-'a'];
  16.             continue;
  17.         }
  18.         init(tot);
  19.         len[tot] = len[cur] + 2;
  20.         nxt[cur][s[i]-'a'] = tot;
  21.         cur = sfx[cur];
  22.         while(s[i-1-len[cur]]!=s[i]) cur = sfx[cur];
  23.         if(len[tot]!=1) sfx[tot] = nxt[cur][s[i]-'a'];
  24.         else sfx[tot] = 1;
  25.         cur = tot++;
  26.     }
  27.     return tot;
  28. }
Add Comment
Please, Sign In to add comment