HDU 2222 Keywords Search(AC自动机模板题)
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自动机模板题)的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- vue学习之node.js
Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...
- linux下面发布80端口的服务
1:linux下面发布端口号为80 的服务,要在root用户下面发布.否则提示权限不够 2:在普通用户下面配置的java环境,在root用户下面不可用. 解决方法:2.1 要使用source /et ...
- jquery closest & parent比较
.closest() .parents() 从当前元素开始 从父元素开始 沿 DOM 树向上遍历,直到找到已应用选择器的一个匹配为止. 沿 DOM 树向上遍历,直到文档的根元素为止,将每个祖先元素添加 ...
- CloudFlare Support - Error 522: Connection timed out 错误522:连接超时
522错误意味着我们无法在所有到达原点Web服务器. 这方面有几个主要原因: 原始服务器太超载回应. 源Web服务器具有挡住了我们的请求的防火墙,或者数据包被主机的网络内下降. 源Web服务器脱机,或 ...
- 初次使用git上传代码(转)
转自 http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我 ...
- jq table页二级联动
<div class="layerRtb layerRtb-threecolumn"> <div class="clearfix layerRtb-he ...
- 性能分析之– JAVA Thread Dump 分析
最近在做性能测试,需要对线程堆栈进行分析,在网上收集了一些资料,学习完后,将相关知识整理在一起,输出文章如下. 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Du ...
- linux常用命令:ss 命令
ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...
- sublime工具安装完成后使用Emmet加快前端页面的开发速度
sublime的安装这里就不介绍了,很多人看到有些人在使用sublime时使用了一些快捷键非常快速的搭建一个简单的html,简单的结构就打起来了.不需要手动一个标签一个标签写. 其实是他们安装了Emm ...
- C/C++之单例模式实现
/*** * 保证一个类仅有一个实例,并提供一个访问它的全局访问点 */ #include <iostream> #include <string> using namespa ...