HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222
AC自动机模板题
我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接:
http://blog.csdn.net/niushuai666/article/details/7002823
http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char key[];
char des[];
struct node{
node *fail;
node *next[];
int cnt;
node(){
fail = NULL;
cnt = ;
for(int i = ;i<;i++)
next[i] = NULL;
}
};
node *root;
void insert(char *str){
node *head = root;
int len = strlen(str);
for(int i = ;i<len;i++){
int temp = str[i]-'a';
if(head->next[temp] == NULL)
head->next[temp] = new node();
head = head->next[temp];
}
head->cnt++;
}
void build(){
queue<node *>q;
q.push(root);
while(!q.empty()){
node *head = q.front();
q.pop();
for(int i = ;i<;i++){
if(head->next[i] != NULL){
if(head == root){
head->next[i]->fail = root;
}else{
node *temp = head->fail;
while(temp != NULL){
if(temp->next[i] != NULL){
head->next[i]->fail = temp->next[i];
break;
}
temp = temp->fail;
}
if(temp == NULL)
head->next[i]->fail = root;
}
q.push(head->next[i]);
}
}
}
}
int query(){
int len = strlen(des),ans = ;;
node *head = root;
for(int i = ;i<len;i++){
int index = des[i]-'a';
while(head->next[index] == NULL && head != root)
head = head->fail;
head = head->next[index];
if(head == NULL)
head = root;
node *temp = head;
while(temp!=root && temp->cnt!=-){
ans += temp->cnt;
temp->cnt = -;
temp = temp->fail;
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
root = new node();
int n;
scanf("%d",&n);
for(int i = ;i<n;i++){
scanf(" %s",key);
insert(key);
}
build();
scanf(" %s",des);
printf("%d\n",query());
}
return ;
}
HDU 2222 AC自动机模板题的更多相关文章
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- UOJ#246. 【UER #7】套路
题目传送门 官方题解传送门 一句话题意的话就是给定一个序列,从中找出至少$k$个连续的元素形成子序列,使得子序列中任意两个元素差值的最小值于其长度-1的乘积最大. 题目中给出了$ 1 \leq a_i ...
- 2 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之MySql的搭建
preface 上一节我们讲了DRBD的原理,以及如何部署DRBD,那么现在在上一节的基础上部署Mysql 安装并启动Mysql 为了方便,我一般采用yum安装Mysql.命令如下: 在172.16. ...
- auto dock
http://mgltools.scripps.edu/ http://mgltools.scripps.edu/downloads/previous-releases/mgltools-1-5.4/ ...
- cx_freeze 把 .py 打包成 .exe
1.安装 python-3.4.3 默认安装路径 C:\Python34 2.安装 cx_Freeze-4.3.3.win32-py3.4 3.运行 Python Version 3.4 regist ...
- Visual Studio 2012中Visual Assist破解办法
本工具有两种破解方式 1.破解方式一:Visual_Assist_X_DiE.exe 运行Visual_Assist_X_DiE.exe,勾选安装的VC版本,点击“Patch”按钮即可. 2.破解方式 ...
- Language Tool ,a plugin for TeXStudio
Language Tool ,a plugin for TeXStudio TexStudio supports LanguageTool as an inline grammar checker. ...
- localdb下载地址
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=29062 只能使用IE下载 http://www.microsoft.co ...
- Python开发【第十二篇】:DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- easyui的textbox和validatebox的 赋值区别
区别代码如下: textbox:$('userId').textbox('setValue','aaa'); validatebox :$('userId').val('aaa');
- FMDB 多线程使用
在App中保持一个FMDatabaseQueue的实例,并在所有的线程中都只使用这一个实例. [FMDatabaseQueue databaseQueueWithPath:path]; FMDatab ...