Advertisement
Jasir

Aho Corasick

Feb 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1.  
  2. const int N = (500 * 500 + 3);
  3. const int CHARS = 26;
  4.  
  5. struct Node {
  6.     int son[CHARS], next, cnt;
  7.     bool end;
  8. };
  9.  
  10. struct AC {
  11.     Node node[N];
  12.     int tol, n, pos[N];
  13.  
  14.     inline int getid(char& x) {
  15.         return x - 'a';
  16.     }
  17.  
  18.     void init() {
  19.         for(int i = 0; i < N; i++) {
  20.             memset(node[i].son, 0, sizeof(node[i].son));
  21.             node[i].next = node[i].cnt = node[i].end = 0;
  22.         }
  23.         tol = 1;
  24.     }
  25.  
  26.     void insert(string& s, int stringIndex) {
  27.         int cur = 0, l = s.size();
  28.         for(int i = 0; i < l; i++) {
  29.             int& p = node[cur].son[getid(s[i])];
  30.             if(!p) p = tol++;
  31.             cur = p;
  32.             if(i == l - 1) {
  33.                 //assert(node[cur].str == " "); //
  34.                 node[cur].end = true;
  35.                 pos[stringIndex] = cur;
  36.             }
  37.         }
  38.     }
  39.  
  40.     void bfs() {
  41.         queue<int> q;
  42.         q.push(0);
  43.         while(!q.empty()) {
  44.             int fa = q.front();
  45.             q.pop();
  46.             if(!fa) {
  47.                 for(int i = 0; i < CHARS; i++) {
  48.                     int s = node[fa].son[i];
  49.                     if(!s) continue;
  50.                     q.push(s);
  51.                 }
  52.             } else {
  53.                 for(int i = 0; i < CHARS; i++) {
  54.                     int s = node[fa].son[i];
  55.                     if(!s) continue;
  56.  
  57.                     int fanext = node[fa].next;
  58.                     while(fanext && !node[fanext].son[i]) fanext = node[fanext].next;
  59.  
  60.                     if(node[fanext].son[i]) node[s].next = node[fanext].son[i];
  61.                     q.push(s);
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     void search(string& s) {
  68.         int cur = 0, l = s.size();
  69.         for(int i = 0; i < l; i++) {
  70.             int id = getid(s[i]);
  71.             while(cur && !node[cur].son[id]) {
  72.                 cur = node[cur].next;
  73.             }
  74.             if(node[cur].son[id]) cur = node[cur].son[id];
  75.  
  76.             int tmp = cur;
  77.             while(tmp) { // important but slowly.
  78.                 if(node[tmp].end) node[tmp].cnt++;
  79.                 tmp = node[tmp].next;
  80.             }
  81.         }
  82.     }
  83.  
  84.     void read() {
  85.         string word;
  86.         for(int i = 0; i < n; i++) {
  87.             cin >> word;
  88.             insert(word, i);
  89.         }
  90.     }
  91.  
  92.     void print() {
  93.         for(int i = 0; i < n; i++) {
  94.             printf("%d\n", node[pos[i]].cnt);
  95.         }
  96.     }
  97.  
  98.     void debug() {
  99.         printf("tol = %d\n", tol);
  100.         int cur = 0;
  101.         for(int i = 0; i < tol; i++) printf("node[%d]->next = node[%d]\n", i, node[i].next);
  102.     }
  103.  
  104. } ac;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement