Keywords Search - HDU 2222(AC自动机模板)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; const int MAXN = 1e6+;
const int MAXM = ;
const int oo = 1e9+; char MumStr[MAXN];///母串 struct Ac_Trie_Node
{///Fail 失败节点
Ac_Trie_Node *Fail, *next[MAXM];
int leaf;///已这个节点为叶子节点的数目
}; void Insert(Ac_Trie_Node *root, char s[])
{///插入所有的模式串
Ac_Trie_Node *p = root; for(int i=; s[i]; i++)
{
int k = s[i]-'a'; if(p->next[k] == NULL)
p->next[k] = new Ac_Trie_Node();
p = p->next[k];
} p->leaf += ;
}
void GetFail(Ac_Trie_Node *root)
{///构造失败指针
queue<Ac_Trie_Node *> Q;
Ac_Trie_Node *p = root, *temp; for(int i=; i<MAXM; i++)
{
if(p->next[i] != NULL)
{
p->next[i]->Fail = root;
Q.push(p->next[i]);
}
} while(Q.size())
{
p = Q.front();Q.pop(); for(int i=; i<MAXM; i++) if(p->next[i])
{///如果节点p的next[i]不为空 temp = p->Fail;///查找另一个最近的next[i]不为空的地方 while(temp != NULL)
{
if(temp->next[i] != NULL)
{///查找到
p->next[i]->Fail = temp->next[i];
break;
} temp = temp->Fail;
} if(temp == NULL)
p->next[i]->Fail = root; Q.push(p->next[i]);
}
}
}
void FreeTrie(Ac_Trie_Node *root)
{///释放内存
Ac_Trie_Node *p = root; for(int i=; i<MAXM; i++)
{
if(p->next[i] != NULL)
FreeTrie(p->next[i]);
} free(p);
}
int Query(Ac_Trie_Node *root)
{
Ac_Trie_Node *p = root, *temp;
int sum=; for(int i=; MumStr[i]; i++)
{
int k = MumStr[i]-'a'; while(!p->next[k] && p != root)
p = p->Fail; if(!p->next[k])continue;///根节点下面没有这个字母 temp = p = p->next[k]; while(temp != root && temp->leaf != -)
{///查找路径上的所有子节串,因为每个子串只出现一次,所以赋值为-1,防止重搜
sum += temp->leaf;
temp->leaf = -;
temp = temp->Fail;
}
} return sum;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int M; char s[];
Ac_Trie_Node *root = new Ac_Trie_Node(); scanf("%d", &M); while(M--)
{
scanf("%s", s);
Insert(root, s);
} GetFail(root); scanf("%s", MumStr);
int ans = Query(root); printf("%d\n", ans); FreeTrie(root);
} return ;
}
Keywords Search - HDU 2222(AC自动机模板)的更多相关文章
- 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 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- 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).问匹配串中含有哪几个模式串 ...
- Keywords Search HDU - 2222 ( ac自动机)模版题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222(ac自动机板题。。)
求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...
- HDU 2222 (AC自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
随机推荐
- xml中报错,验证是否是xml报错
1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[ ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...
- oracle decode函数使用方法
1.decode(V1,1,A,2,B,C) 如果V1=1 那么显示A =2显示B 其他显示C ........ 2. 含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返 ...
- cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能
cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画 一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部 ...
- JavaScript 字符串(String) 对象
JavaScript 字符串(String) 对象 String 对象用于处理已有的字符块. JavaScript 字符串 一个字符串用于存储一系列字符就像 "John Doe". ...
- Linux下JDK环境变量配置
JDK官方下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 我的下载路 ...
- Unity3d脚本的生命周期
接下来,做出一下讲解:最先执行的方法是Awake,这是生命周期的开始,用于进行激活时的初始化代码,一般可以在这个地方将当前脚本禁用:this.enable=false,如果这样做了,则会直接跳转到On ...
- 实用的透明背景mark图标
- 分享29个超赞的响应式Web设计
原文自:http://www.csdn.net/article/2013-01-16/2813678-responsive-design-websites 最近几年,响应式Web设计不断印入人们眼帘, ...
- BZOJ 1033 杀蚂蚁
Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...
- HYPER-V2008里安装WINDOWS 2012,以及监控宝
呵呵,这两者有关系么?没关系.哈哈. 为了方便就放一起了. 至少,2008里的HYPERV能安装2012,倒是给我很多欣慰.