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 ...
随机推荐
- JavaScript - 测试 jQuery
测试 JavaScript 框架库 - jQuery 引用 jQuery 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 <script> 标签,其 s ...
- Struts2中使用execAndWait后,在 Action中调用getXXX()方法报告java.lang.NullPointerException异常的原因和解决方法
使用 Struts2 编写页面,遇到一个要长时间运行的接口,因此增加了一个execAndWait ,结果在 Action 中调用 getContext()的时候报告异常 ActionContext c ...
- 阿里云的esc
云服务器ecs作用如下:1.完全管理权限:对云服务器的操作系统有完全控制权,用户可以通过连接管理终端自助解决系统问题,进行各项操作:2.快照备份与恢复:对云服务器的磁盘数据生成快照,用户可使用快照回滚 ...
- 专家解读Linux操作系统内核中的GCC特性
专家解读Linux操作系统内核中的GCC特性 Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...
- BZOJ 3223 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- [BZOJ 1801] [Ahoi2009]chess 中国象棋 【DP】
题目链接:BZOJ - 1801 题目分析 对于50%的数据是可以直接状压 DP 的. 对于100%的数据,使用递推的 DP .(或者这只叫递推不叫 DP ?) 可以发现,每一行和每一列的棋子个数不能 ...
- bit、sbin、sfr、sfr16 区别分析
1.bit 和 sbit 都是 C51 扩展的变量类型. bit 和 int char 之类的差不多,只不过 char=8 位, bit=1 位而已.都是变量,编译器在编译过程中分配地址.除非你指定, ...
- 通过CreateOleObject控制IE
//第二种方法可以有更多控制procedure TForm1.Button1Click(Sender: TObject);procedure OpenInIE(aURL: string);//need ...
- win平台下, 检测网络是否连接最好的办法
[Delphi]检查URL是否有效的函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function CheckUr ...
- centos 6.5关闭NetworkManager
jrhmpt01:/root# rpm -qa | grep -i network NetworkManager-glib-0.8.1-99.el6.x86_64 system-config-netw ...