http://acm.hdu.edu.cn/showproblem.php?pid=2222

题意:
给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词。

思路:

AC自动机的模板题。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n;
int num; struct Trie
{
int son[];
int cnt;
int fail;
}t[*maxn]; void init(int x)
{
t[x].cnt=t[x].fail=;
memset(t[x].son,,sizeof(t[x].son));
} void trie(char *s)
{
int n=strlen(s);
int x=;
for(int i=;i<n;i++)
{
int c=s[i]-'a'+;
if(!t[x].son[c])
{
num++;
init(num);
t[x].son[c]=num;
}
x=t[x].son[c];
}
t[x].cnt++;
} void buildAC()
{
queue<int> Q;
for(int i=;i<=;i++) if(t[].son[i]) Q.push(t[].son[i]);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
int fail=t[x].fail;
for(int i=;i<=;i++)
{
int y=t[x].son[i];
if(y)
{
t[y].fail=t[fail].son[i];
Q.push(y);
}
else t[x].son[i]=t[fail].son[i];
}
}
} int query(char *s)
{
int n=strlen(s);
int x=, ans=;
for(int i=;i<n;i++)
{
int c=s[i]-'a'+;
while(x && !t[x].son[c]) x=t[x].fail;
x=t[x].son[c];
int tmp=x;
while(tmp && t[tmp].cnt!=-)
{
ans+=t[tmp].cnt;
t[tmp].cnt=-;
tmp=t[tmp].fail;
}
}
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
char s[];
scanf("%d",&T);
while(T--)
{
num=;
init();
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",s);
trie(s);
}
buildAC();
scanf("%s",s);
printf("%d\n",query(s));
}
return ;
}

HDU 2222 Keywords Search(AC自动机模板题)的更多相关文章

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

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  6. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  7. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  8. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  9. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  10. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. vertx连接mysql数据库

    1:创建一个verticle组件 package jdbcConnection; import io.vertx.core.AbstractVerticle; import io.vertx.core ...

  2. Tomcat重启session失效

    在Tomcat的目录下找到context.xml,取消掉<Manager pathname="" /> 这句的注释.

  3. 网站被XMR恶意挖矿

    发现: 网站首页被恶意更改 网站的关键词和描述被恶意更改 服务器和只要访问此服务器上的网页cup 直线上升100% 排查代码发现js 文件被恶意更改,访问了挖矿网站 操作:删除js 里面的恶意代码,更 ...

  4. input标签file文件上传图片本地预览

    <input type="file" name="img-up" id="img-up" value="" /&g ...

  5. Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class

    转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Colle ...

  6. Qt事件过滤器和事件的发送

    事件过滤器 /* *事件过滤器不是类,仅仅是QObject类的两个函数:installEventFilter() 和 eventFilter() . *下面讲个例子: * 监视过滤 textEdit的 ...

  7. 修改SQL Server 的排序规则(转)

    转自http://jimshu.blog.51cto.com/3171847/1095780/ 一.修改SQL Server服务器(实例)的排序规则 以下实验使用了SQL Server 2008 R2 ...

  8. Rpgmaker开发心得(1)且事件

    例:NPC让你给他桃子和梨子,然后给你西瓜. 实际就是:有桃子且有梨子时的判断,但对于大多数不会直接编写脚本的同学,最好的方式就是使用开关. 思路如下: var:变量 on:开关 if(on西瓜=on ...

  9. Neutron网络学习

    学习 Neutron 系列文章: 转http://www.cnblogs.com/sammyliu/p/4622563.html (1)Neutron 所实现的网络虚拟化 (2)Neutron Ope ...

  10. HTTP 协议入门

    本文转载自:http://www.ruanyifeng.com/blog/2016/08/http.html HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它 ...