HDU2222 Keywords Search(AC自动机模板)
AC自动机是一种多模式匹配的算法。大概过程如下:
- 首先所有模式串构造一棵Trie树,Trie树上的每个非根结点都代表一个从根出发到该点路径的字符串。
- 然后每个结点都计算出其fail指针的值,这个fail指针就指向这个结点所表示字符串的最长存在的后缀所对应的结点,如果不存在就指向根:计算每个结点的fail用BFS,比如当前结点u出队要拓展并计算其孩子结点的fail,v是其第k个孩子,fail[v]的值就是某个fail[fail[fail...[u]]]存在第k孩子结点其第k个孩子结点,如果不存在fail[v]就等于root。
- 最后主串就往Trie树上跑,在某个Trie树结点失配了就跳转到这个结点fail指针所指的结点继续跑——不过如果匹配了某个模式串这时可能某个模式串的后缀串被忽略了,所以需要用到temp指针,去检查是否有遗漏后缀没匹配。
而这题大概就是给几个模式串,一个主串,问有几个模式串被主串匹配。
AC自动机的模板题。有个可以优化的地方就是某个模式串被匹配了,下一次经过这儿就可以跳过了temp指针的过程了。
代码参考自kuangbin巨的博客,太简洁了(300+ms):
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int tn,ch[][],cnt[],fail[];
void insert(char *s){
int x=;
for(int i=; s[i]; ++i){
int y=s[i]-'a';
if(ch[x][y]==) ch[x][y]=++tn;
x=ch[x][y];
}
++cnt[x];
}
void init(){
memset(fail,,sizeof(fail));
queue<int> que;
for(int i=; i<; ++i){
if(ch[][i]) que.push(ch[][i]);
}
while(!que.empty()){
int x=que.front(); que.pop();
for(int i=;i<;++i){
if(ch[x][i]) que.push(ch[x][i]),fail[ch[x][i]]=ch[fail[x]][i];
else ch[x][i]=ch[fail[x]][i];
}
}
}
int query(char *s){
int x=,res=;
for(int i=; s[i]; ++i){
int tmp=x=ch[x][s[i]-'a'];
while(tmp){
if(cnt[tmp]>=){
res+=cnt[tmp];
cnt[tmp]=-;
}else break;
tmp=fail[tmp];
}
}
return res;
}
char S[],T[];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
tn=;
memset(ch,,sizeof(ch));
memset(cnt,,sizeof(cnt));
scanf("%d",&n);
while(n--){
scanf("%s",T);
insert(T);
}
init();
scanf("%s",S);
printf("%d\n",query(S));
}
return ;
}
另外之前学的指针版本的,指针版本跑得更快(200+ms):
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef struct Node *pNode;
struct Node{
int cnt;
pNode fail,nxt[];
Node(){
cnt=; fail=NULL;
for(int i=;i<;++i) nxt[i]=NULL;
}
};
pNode root;
char S[];
void insert(char *s){
pNode p=root;
for(int i=;s[i];++i){
int index=s[i]-'a';
if(p->nxt[index]==NULL){
p->nxt[index]=new Node;
}
p=p->nxt[index];
}
++p->cnt;
}
void init(){
queue<pNode> que;
que.push(root);
while(que.size()){
pNode y=que.front(); que.pop();
for(int i=;i<;++i){
if(y->nxt[i]==NULL) continue;
if(y==root){
y->nxt[i]->fail=root;
que.push(y->nxt[i]);
continue;
}
pNode x=y->fail;
while(x&&x->nxt[i]==NULL) x=x->fail;
if(x==NULL) y->nxt[i]->fail=root;
else y->nxt[i]->fail=x->nxt[i];
que.push(y->nxt[i]);
}
}
}
int query(){
int res=;
pNode x=root;
for(int i=;S[i];++i){
int index=S[i]-'a';
while(x->nxt[index]==NULL&&x!=root) x=x->fail;
x=x->nxt[index];
if(x==NULL) x=root;
pNode y=x;
while(y!=root){
if(y->cnt>=){
res+=y->cnt;
y->cnt=-;
}else break;
y=y->fail;
}
}
return res;
}
int main(){
int t,n;
char s[];
scanf("%d",&t);
while(t--){
root=new Node;
scanf("%d",&n);
for(int i=;i<n;++i){
scanf("%s",s);
insert(s);
}
scanf("%s",S);
init();
printf("%d\n",query());
}
return ;
}
HDU2222 Keywords Search(AC自动机模板)的更多相关文章
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
随机推荐
- HNU 12826 Balloons Colors
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12826&courseid=268 #include&l ...
- UNITY3D与iOS交互解决方案
原地址:http://bbs.18183.com/thread-456979-1-1.html 本帖最后由 啊,将进酒 于 2014-2-27 11:17 编辑 “授人以鱼,不如授人以渔”,以UNIT ...
- FlashDevelop快捷键
将鼠标点到变量上面后,同时按ctrl+shift+1(左键盘),可以自动添加变量或者函数.复制一行代码.CTRL+D:ctrl+shift+k 颜色代码拾取器 ctrl+shift+b 注释年选代码段 ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- codeforces A. Flipping Game 解题报告
题目链接:http://codeforces.com/problemset/problem/327/A 题意是输入一个只有0和1的序列,要求找出一个合理的区间,在这个区间里面把0变成1,1变成0,使得 ...
- [Android UI] Shape详解 (GradientDrawable)
转载自:http://blog.csdn.net/feng88724/article/details/6398193 在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来 ...
- July 29th, Week 31st Friday, 2016
I am a slow walker, but I never walk backwards. 我走得很慢,但我从来不会后退. I had run very fast, and I had once ...
- July 21st, Week 30th Thursday, 2016
What youth deemed crystal, age finds out was dew. 年少时的水晶,在岁月看来不过是露珠. As time goes by, we are gradual ...
- Myeclipse中全部文件设置成UTF-8
如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...
- 用mtrace检查内存泄漏
http://blog.csdn.net/ixidof/article/details/6638066内存泄漏检查方法(for Linux) 如果你更想读原始文档, 请参考glibc info的&qu ...