zoukankan      html  css  js  c++  java
  • LA 4670 (AC自动机 模板题) Dominating Patterns

    AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目。=_=||

    AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配。

    因为有多个模式串构成了一颗Tire树,不能像以前一样线性递推失配函数f了,于是改成了BFS求失配函数。

    白书上那个last数组(后缀链接)的含义就是:在Tire树的某个分支上虽然没有遇到单词节点,但是某个单词可能是已经匹配上的字串的后缀。

    举个栗子:

    有两个模式串:aaabbb, ab

    现在已经匹配了aaab,现在的位置正在Tire树上aaabbb的分支上,明显没有遇到单词节点,但是注意到ab是aaab的后缀,也就是说我们虽然没有匹配到第一个模式串,但是找到第二个模式串ab了。

    这就是last数组的作用。

      1 #include <cstdio>
      2 #include <string>
      3 #include <cstring>
      4 #include <queue>
      5 #include <map>
      6 using namespace std;
      7 
      8 const int SIGMA_SIZE = 26;
      9 const int MAXNODE = 11000;
     10 const int MAXS = 150 + 10;
     11 
     12 map<string, int> ms;
     13 
     14 struct AhoCorasickAutomata
     15 {
     16     int ch[MAXNODE][SIGMA_SIZE];
     17     int f[MAXNODE];    //失配函数
     18     int val[MAXNODE];
     19     int last[MAXNODE];
     20     int cnt[MAXS];
     21     int sz;
     22 
     23     void init()
     24     {
     25         memset(ch[0], 0, sizeof(ch[0]));
     26         memset(cnt, 0, sizeof(cnt));
     27         ms.clear();
     28         sz = 1;
     29     }
     30 
     31     inline int idx(char c) { return c - 'a'; }
     32 
     33     //插入字符串s, v > 0
     34     void insert(char* s, int v)
     35     {
     36         int u = 0, n = strlen(s);
     37         for(int i = 0;  i < n; i++)
     38         {
     39             int c = idx(s[i]);
     40             if(!ch[u][c])
     41             {
     42                 memset(ch[sz], 0, sizeof(ch[sz]));
     43                 val[sz] = 0;
     44                 ch[u][c] = sz++;
     45             }
     46 
     47             u = ch[u][c];
     48         }
     49         val[u] = v;
     50         ms[string(s)] = v;
     51     }
     52 
     53     //统计每个字串出现的次数
     54     void count(int j)
     55     {
     56         if(j)
     57         {
     58             cnt[val[j]]++;
     59             count(last[j]);
     60         }
     61     }
     62 
     63     //在T中查找模板
     64     void find(char* T)
     65     {
     66         int j = 0, n = strlen(T);
     67         for(int i = 0; i < n; i++)
     68         {
     69             int c = idx(T[i]);
     70             while(j && !ch[j][c]) j = f[j];
     71             j = ch[j][c];
     72             if(val[j]) count(j);
     73             else if(last[j]) count(last[j]);
     74         }
     75     }
     76 
     77     //计算失配函数
     78     void getFail()
     79     {
     80         queue<int> q;
     81         f[0] = 0;
     82         for(int i = 0; i < SIGMA_SIZE; i++)
     83         {
     84             int u = ch[0][i];
     85             if(u) { last[u] = 0; f[u] = 0; q.push(u); }
     86         }
     87         while(!q.empty())
     88         {
     89             int r = q.front(); q.pop();
     90             for(int c = 0; c < SIGMA_SIZE; c++)
     91             {
     92                 int u = ch[r][c];
     93                 if(!u) continue;
     94                 q.push(u);
     95                 int v = f[r];
     96                 while(v && !ch[v][c]) v = f[v];
     97                 f[u] = ch[v][c];
     98                 last[u] = val[f[u]] ? f[u] : last[f[u]];
     99             }
    100         }
    101     }
    102 }ac;
    103 
    104 const int maxn = 1000000 + 10;
    105 char T[maxn], P[160][80];
    106 
    107 int main()
    108 {
    109     //freopen("in.txt", "r", stdin);
    110 
    111     int n;
    112     while(scanf("%d", &n) == 1 && n)
    113     {
    114         ac.init();
    115         for(int i = 1; i <= n; i++) { scanf("%s", P[i]); ac.insert(P[i], i); }
    116         scanf("%s", T);
    117         ac.getFail();
    118         ac.find(T);
    119         int M = -1;
    120         for(int i = 1; i <= n; i++) if(ac.cnt[i] > M) M = ac.cnt[i];
    121         printf("%d
    ", M);
    122         for(int i = 1; i <= n; i++)
    123             if(ac.cnt[ms[string(P[i])]] == M)
    124                 printf("%s
    ", P[i]);
    125     }
    126 
    127     return 0;
    128 }
    代码君
  • 相关阅读:
    apue 第19章 伪终端
    apue 第18章 终端I/O
    linux IPC socket(2)
    linux IPC socket
    linux POSIX信号量
    【Luogu】【关卡2-16】线性动态规划(2017年10月)【还差三道题】
    【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】
    【Luogu】【关卡2-14】 树形数据结构(2017年10月)【AK】
    【Luogu】【关卡2-13】线性数据结构(2017年10月)【还差一道题】
    【Luogu】【关卡2-12】递推与递归二分(2017年10月)
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4392890.html
Copyright © 2011-2022 走看看