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\n", M);
122         for(int i = 1; i <= n; i++)
123             if(ac.cnt[ms[string(P[i])]] == M)
124                 printf("%s\n", P[i]);
125     }
126
127     return 0;
128 }

代码君

时间: 2024-08-25 23:05:00

LA 4670 (AC自动机 模板题) Dominating Patterns的相关文章

LA 4670 出现次数最多的子串 (AC自动机模板题)

Dominating Patterns Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; ea

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

HDU 2222 Keywords Search(AC自动机模板题)

原题大意:原题链接 先给定T个单词,然后给定一个字符串,查询该字符串中包含多少个给定的单词 解题思路:AC自动机模板题 参考链接:哔哩哔哩算法讲堂 WA版本 注意:因为输入的单词可能有重复,那么Insert()函数中p->id=id;语句中p->id会被覆盖,在Query()函数中会一次性全部被清零,导致不能查询重复单词,以至于结果res错误. #include<queue> #include<cstdio> #include<cstring> using

HDU 2222(AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题思路: AC自动机模板题. 一开始使用LRJ的坑爹静态模板,不支持重复的模式串. 在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板, 搞出了这么一个支持重复串的模板. #include "cstdio" #include

NYOJ 1085 数单词 (AC自动机模板题)

数单词 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 为了能够顺利通过英语四六级考试,现在大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中可以通过六级,可是没想到,成绩出来以后,居然没有通过.所以他不得不付出更多的时间来学习英语. 要想通过六级,最基本的要求就是词汇量.为了能够更快的记住一些陌生单词,LYH有时会找一些英语文章来读. 今天早上,LYH又找了一篇文章.读之前,他突然萌生出一个想法:文章中哪些单词出现的次数最多呢? 输入 第一行输入一个

HDU3695(AC自动机模板题)

题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<st

HDU 2222 AC自动机模板题

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 char key[55]; 6 char des[1111111]; 7 struct node{ 8 node *fail; 9 node *next[26]; 10 int cnt;

HDU 3065 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模式串,且每种模式串出现了多少次. 解题思路: AC自动机模板题.模式串的范围是大写字母,但是匹配串的范围却是(0~127). 如果Trie 开到 128 加上不回收内存,就会MLE. 实际上开到26就行了,find的时候对于c<0||c>26,强制令pos=root出现失配,并开始下一个字符就行了

HDU 2896 (AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串. 解题思路: AC自动机模板题.注意一下字符范围. cnt记录这个模式串的个数改为这个模式串的index. find的时候,把找到的index压入vector里面即可. 注意有多个匹配串,每次find之后会把last->cnt修改,原因是防止一个模式串出现了多次被压入vector,所以先备份一下,