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 ...
随机推荐
- Node中Exports与module.export的使用与区别
最近在看<node开发实战详解>时有写疑问,所以自己就整理了一些资料.下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html ...
- Maven搭建Spring+Struts2+Hibernate项目详解
http://www.bubuko.com/infodetail-648898.html
- OC - 29.自定义布局实现瀑布流
概述 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UIColl ...
- 绘图quartz之阴影
//设置矩形的阴影 并在后边加一个圆 不带阴影 步骤: CGContextRef context = UIGraphicsGetCurrentContext(); ...
- Tomcat虚拟主机配置
3.1.配置虚拟主机 配置虚似主机就是配置一个网站. 在Tomcat服务器配置一个虚拟主机(网站),需要修改conf文件夹下的server.xml这个配置文件,使用Host元素进行配置,打开serve ...
- WAMP学习日记之:Apache发布php网站
1.修改httpd.conf 文件,以便让Apache和php模块建立关联 2.配置php.ini文件 修改httpd.conf 文件,以便让Apache和php模块建立关联 如何让apache和ph ...
- UFLDL实验报告1: Softmax Regression
PS:这些是今年4月份,跟斯坦福UFLDL教程时的实验报告,当时就应该好好整理的…留到现在好凌乱了 Softmax Regression实验报告 1.Softmax Regression实验描述 So ...
- 删除svn文件
删除svn文件 sudo find . -name ".DS_Store" -exec rm -r {} \; sudo find . -name ".git" ...
- eclipse 比较好的插件
tomcat 插件 egit github 插件 subeclipse 插件 Properties Editor Properties Editor 编辑java的属性文件,并可以自动存盘为Unico ...
- jQuery取CSS的HEX(16位)颜色值
//IE10以上版本.CHROME.FIREFOX中GRB颜色转HEX颜色值 $.fn.getBackgroundColor = function() { var rgb = $(this).css( ...