Advertisement
K_Y_M_bl_C

Untitled

Mar 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. struct Trie
  2. {
  3.     struct Node
  4.     {
  5.         int cnt;
  6.         int suf;
  7.         vi go;
  8.         Node() { cnt = 0, suf = 0, go.assign(26, -1); };
  9.     };
  10.     vector<Node> t;
  11.     Trie() { t.inb(Node()); };
  12.     int adds(string s)
  13.     {
  14.         int u = 0;
  15.         for (int i = 0; i < (int)s.size(); ++i)
  16.         {
  17.             if (t[u].go[s[i] - 'a'] == -1)
  18.             {
  19.                 t[u].go[s[i] - 'a'] = t.size();
  20.                 t.inb(Node());
  21.             }
  22.             u = t[u].go[s[i] - 'a'];
  23.         }
  24.         return u;
  25.     }
  26.     void buildAK()
  27.     {
  28.         queue<int> q;
  29.         q.push(0);
  30.         while (!q.empty())
  31.         {
  32.             int u = q.front();
  33.             q.pop();
  34.             for (int i = 0; i < 26; ++i)
  35.             {
  36.                 int to = 0;
  37.                 if (u)
  38.                     to = t[t[u].suf].go[i];
  39.                 if (t[u].go[i] != -1)
  40.                 {
  41.                     t[t[u].go[i]].suf = to;
  42.                     q.push(t[u].go[i]);
  43.                 }
  44.                 else
  45.                 {
  46.                     t[u].go[i] = to;
  47.                 }
  48.             }
  49.         }
  50.     }
  51. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement