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 ...
随机推荐
- PPTP服务器配置选项详解
导读 PPTP服务器配置文件的格式与其它许多Unix程序相似,每一行包含一项配置内容,以配置选项名称开始,后面紧跟参数值或者关键字,它们之间用空格分隔.在读取配置文件时,pptpd进程将忽略空行和每一 ...
- [codeforces 241]A. Old Peykan
[codeforces 241]A. Old Peykan 试题描述 There are n cities in the country where the Old Peykan lives. The ...
- nginx(三)初步搭建nginx虚拟主机
上面就是nginx基于域名.ip访问的配置,掌握住格式,就很好配置了. 一.基于域名的虚拟主机的配置:1.我们在此复习一下DNS的配置:[root@mgmserver /]# hostnamemgms ...
- [ruby on rails] 跟我学之(8)修改数据
修改views 修改index视图(app/views/posts/index.html.erb),添加编辑链接,如下: <h1>Our blogs</h1> <% @p ...
- 推荐一个linux下的web压力测试工具神器webbench
推荐一个linux下的web压力测试工具神器webbench2014-04-30 09:35:29 来源: 评论:0 点击:880 用多了apache的ab工具之后你就会发现ab存在很多问题, ...
- XmlWriter/XmlReader示例代码
在Silverlight项目中,如果您想最大程度的减少xap包的大小,仅使用默认System.Xml命名空间下提供的功能来实现“XML序列化/反序列化”,恐怕XmlReader/XmlWriter将成 ...
- Android 中PendingIntent---附带解决AlarmManager重复加入问题
最近在程序中使用到了notification功能,自然,就涉及到了PendingIntent,下面总结下. 1 什么是PendingIntent A description of an Intent ...
- Ribbon_窗体_实现Ribbon风格的窗体
Ribbon_窗体_实现Ribbon风格的窗体 随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢? ...
- codeforces 486B.OR in Matrix 解题报告
题目链接:http://codeforces.com/problemset/problem/486/B 题目意思:给出一个m行n列的矩阵B(每个元素只由0/1组成),问是否可以利用矩阵B,通过一定的运 ...
- [Android Pro] android Flag介绍
一些Flag的介绍 窗口之后的内容变暗. public static final int FLAG_DIM_BEHIND = 0x00000002; 窗口之后的内容变模糊. public ...