HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串。
思路:用int型的end数组记录出现,AC自动机即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#define maxn 500005
int T,n;
char s[];
struct Trie{
int root,L;
int next[maxn][],fail[maxn],end[maxn];
int newnode(){
for (int i=;i<;i++)
next[L][i]=-;
end[L++]=;
return L-;
}
void clear(){
L=;root=newnode();
}
void insert(char s[]){
int now=root,len=strlen(s);
for (int i=;i<len;i++){
if (next[now][s[i]-'a']==-) next[now][s[i]-'a']=newnode();
now=next[now][s[i]-'a'];
}
end[now]++;
}
void build(){
std::queue<int>Q;
int now=root;
for (int i=;i<;i++)
if (next[now][i]==-) next[now][i]=root;
else
fail[next[now][i]]=root,Q.push(next[now][i]);
while (!Q.empty()){
int now=Q.front();
Q.pop();
for (int i=;i<;i++){
if (next[now][i]==-)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int query(char s[]){
int now=root,len=strlen(s),res=;
for (int i=;i<len;i++){
now=next[now][s[i]-'a'];
int tmp=now;
while (tmp!=root){
res+=end[tmp];
end[tmp]=;
tmp=fail[tmp];
}
}
return res;
}
}ac;
int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
ac.clear();
for (int i=;i<n;i++){
scanf("%s",s);
ac.insert(s);
}
ac.build();
scanf("%s",s);
printf("%d\n",ac.query(s));
}
}
HDU 2222 Keywords Search (AC自动机)的更多相关文章
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- 功率和dB的关系
功率和dB的关系应该如下: 1.dB的引入是为了把乘除关系变换为加减,便于工程中的运算. 2.[dB] = 10lg(输出功率W/输入功率W).如:输入功率为1W而输出功率为1000W,则系统的增益为 ...
- sql中update,alter,modify,delete,drop的区别和使用(整理)(转)
关于update和alter: 百度知道上关于update和alter有一个很形象的总结: 一个表有很多字段,一个字段里有很多数据. 一个家有很多房间,一个房间里有很多家具. update是用来将衣柜 ...
- Computer Graphics Thinking–texture tiling
Here is one question: how to tile texture? One thing worth to notice: The DirectX and OpenGL stipula ...
- window.open和window.close的使用详解
有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢? 我们知道window.open可以新 ...
- Linux 内存管理知识学习总结
现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的.对于内存部分需要知道: 地址映射 内存管理的方式 缺页异常 先来看一些基本的知识,在进程看来,内 ...
- PHP数据结构:栈、队列、堆、固定数组
数据结构:栈 队列: 堆: 固定尺寸的数组:
- java集合类之------Properties
之前经常看到有人在网上问关于HashMap 和Hashtable 的区别,自己也在看,时间一长发现自己也忘了二者的区别,于是在实际应用中犯错了. 原因是使用了Properties 这个集合类时将nul ...
- actionBar兼容2.1及以上版本的做法 .
正在准备一个项目,需要尊重android design的同时还要做到很好的兼容低版本,于是就先从actionBar开始吧. 1,新建一个android工程startActionBar,minSdkVe ...
- 原生 javascript 学习之 js变量
1.变量的命名 方法的命名(驼峰命名法) 全部小写 : 单词与单词之间全部下划线 (my_namespace) 大小写混合 : 第一个单词首字母小写其他单词首字母大写. 规则 首字符 英文字母或下划线 ...
- Oracle 用户、对象权限、系统权限
--================================ --Oracle 用户.对象权限.系统权限 --================================ 一.用户与模式 ...