AC自动机模板 LA4670 Dominating Patterns 出现次数最多的字串 BNUOJ11552 UVA1449
2015-05-27 22:35
525 查看
题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=11552
The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these
patterns may appear more than one times in a large text string (also only lower case English letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.
It is your job to find the dominating pattern(s) and their appearing times.
The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N, 1
![](http://www.bnuoj.com/v3/external/46/4670img1.png)
N
![](http://www.bnuoj.com/v3/external/46/4670img1.png)
150.
Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the
text to lookup, whose length is up to 106.
At the end of the input file, number `0' indicates the end of input file.
For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.
有n个小写字母组成的字符串和一个文本串T,找出哪些字符串在T中出现的次数最多。
AC自动机,代码来自刘汝佳的大白书
The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these
patterns may appear more than one times in a large text string (also only lower case English letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.
It is your job to find the dominating pattern(s) and their appearing times.
Input
The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N, 1![](http://www.bnuoj.com/v3/external/46/4670img1.png)
N
![](http://www.bnuoj.com/v3/external/46/4670img1.png)
150.
Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the
text to lookup, whose length is up to 106.
At the end of the input file, number `0' indicates the end of input file.
Output
For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.
Sample Input
2 aba bab ababababac 6 beta alpha haha delta dede tata dedeltalphahahahototatalpha 0
Sample Output
4 aba 2 alpha haha
AC自动机,代码来自刘汝佳的大白书
//有n个小写字母组成的字符串和一个文本串T,找出哪些字符串在T中出现的次数最多。 //AC自动机,代码来自刘汝佳的大白书 #include<cstring> #include<queue> #include<cstdio> #include<map> #include<string> using namespace std; const int SIGMA_SIZE = 26; const int MAXNODE = 11000; const int MAXS = 150 + 10; map<string,int>ms; struct AhoCorasickAutomata { int ch[MAXNODE][SIGMA_SIZE]; int f[MAXNODE];//fail数组 int val[MAXNODE];//val>0就是单词节点 int last[MAXNODE];//链表的下一个节点 int cnt[MAXS]; int sz;//节点总数 void init(){ sz=1;//初始时一个根节点 memset(ch[0],0,sizeof(ch[0])); memset(cnt,0,sizeof(cnt)); ms.clear(); } int idx(char c){ return c-'a'; } //插入字符串。v必须非0 void Insert(char *s,int v){ int u=0,n=strlen(s); for(int i=0;i<n;i++){ int c=idx(s[i]); if(!ch[u][c]){//节点不存在 memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0;//中间节点是0 ch[u][c]=sz++;//新建节点 } u=ch[u][c];//往下走 } val[u]=v;//字符串最后一个字符标志信息为v ms[string(s)]=v; } //递归打印以节点j结尾的所有字符串 void print(int j){ if(j){ cnt[val[j]]++; print(last[j]); } } //在T中找模板 int Find(char *T){ int n = strlen(T); int j=0;//初试为根,当前节点编号 for(int i=0;i<n;i++){ //文本串当前指针 int c=idx(T[i]); while(j&&!ch[j][c]) j=f[j];//顺着失配走,直到可以匹配到 j=ch[j][c]; if(val[j]) print(j); else if(last[j]) print(last[j]);//找到了 } } void getFail(){ queue<int>q; f[0]=0; for(int c=0;c<SIGMA_SIZE;c++){//初始化队列 int u=ch[0][c]; if(u){ f[u]=0;q.push(u);last[u]=0; } } while(!q.empty()){ int r=q.front();q.pop(); for(int c=0;c<SIGMA_SIZE;c++){ int u=ch[r][c]; if(!u) continue; q.push(u); int v=f[r]; while(v&&!ch[v][c]) v=f[v]; f[u]=ch[v][c]; last[u]=val[f[u]]?f[u]:last[f[u]]; } } } }; AhoCorasickAutomata ac; char text[1000005],P[151][80]; int n,T; int main() { while(scanf("%d", &n) == 1 && n) { ac.init(); for(int i=1;i<=n;i++){ scanf("%s",P[i]); ac.Insert(P[i],i);//注意同时更新ms映射 } ac.getFail(); scanf("%s",text); ac.Find(text);//计算每个模板的cnt int best = -1; for(int i=1;i<=n;i++) if(ac.cnt[i]>best) best=ac.cnt[i];//最大值 printf("%d\n",best); for(int i=1;i<=n;i++) if(ac.cnt[ms[string(P[i])]]==best) printf("%s\n",P[i]);//输出那些模板串 } return 0; }
相关文章推荐
- 出现次数最多的单词 AC自动机 UVA 1449 Dominating Patterns
- AC自动机模板 LA4670
- 求一个字符串中连续出现次数最多的字串
- 试题名称: 出现次数最多的数 已ac
- 求一个字符串中连续出现次数最多的字串.[面试宝典14.5节]
- 后缀自动机 重复旋律 6(长度为K的旋律中出现次数最多的旋律的出现次数)
- POJ 3461 KMP 模板题 字串出现次数
- 字符串中连续出现次数最多的字串
- 找出一行字符串中的连续出现次数最多的字串
- UVA11019 Matrix Matcher(一种计算子矩阵在母矩阵中出现次数的办法,AC自动机+kmp)
- 求一个字符串中连续出现次数最多的字串
- SPOJ8222(后缀自动机--出现次数最多的子串)
- 例题3.14 出现次数最多的子串 LA4670
- 求一个字符串中连续出现次数最多的字串 c++实现方法
- hdu5384(AC自动机+纪录重复单词出现的次数)
- zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)
- 求一个字符串中连续出现次数最多的字串
- UVA 1449 Dominating Patterns (LA4670) 出现次数最多的子串 ac自动机
- javascript求出现最多的字符次数
- hdu 1251 字典树模板题 ---多串 查找单词出现次数