HDU2222Keywords Search AC_自动机
http://blog.csdn.net/niushuai666/article/details/7002823
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int count;
node *next[26],*fail;
void Node(){
count = 0;fail = NULL;
memset(next,NULL,sizeof(next));
}
}arr[300005],*que[500005];
char str[1000006];
int cnt = 0;
void insert(char *str){
node *p = &arr[0];
while( *str ){
int k = *str++ - 'a';
if( !p->next[k] ){
arr[++cnt].Node();
p->next[k] = &arr[cnt];
}
p = p->next[k];
}
p->count++;
} void build_AC(){
node *root = &arr[0],*tmp,*p;
int head = 0,tail = 0;
for(int i = 0; i < 26; i++){
if(!root->next[i])continue;
root->next[i]->fail = root;
que[head++] = root->next[i];
}
while( head != tail){
tmp = que[tail++];
for(int i = 0; i < 26 ; i++){
if( tmp->next[i] ){
p = tmp->fail;
while( p ){
if( p->next[i] ){
tmp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(!p)tmp->next[i]->fail = root;
que[head++] = tmp->next[i];
}
}
}
}
int query(char *str){
node *root = &arr[0],*p = &arr[0];
int ans= 0;
while( *str ){
int k = *str++ - 'a';
while( !p->next[k] && p != root) p = p->fail;
p = p->next[k];
p = p ? p : root;
node *tmp = p;
while( tmp != root && tmp->count != -1){
ans += tmp->count;
tmp->count = -1;
tmp = tmp->fail;
}
}
return ans;
}
int main(){
// freopen("in.txt","r",stdin);
int t,n;
char ch[60];
scanf("%d",&t);
while( t-- ){
scanf("%d",&n);
arr[cnt = 0].Node();
while( n-- ){
scanf("%s",ch);
insert(ch);
}
build_AC();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}
HDU2222Keywords Search AC_自动机的更多相关文章
- 2014 网选 5007 Post Robot(暴力或者AC_自动机(有点小题大作了))
//暴力,从每一行的开始处开始寻找要查询的字符 #include<iostream> #include<cstdio> #include<cstring> #inc ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- 【hdu2222-Keywords Search】AC自动机基础裸题
http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu_2222_Keywords Search(AC自动机板子)
题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- 最近很火的MySQL:抛开复杂的架构设计,MySQL优化思想基本都在这
优化一览图 优化 笔者将优化分为了两大类:软优化和硬优化.软优化一般是操作数据库即可:而硬优化则是操作服务器硬件及参数设置. 1.软优化 1)查询语句优化 首先我们可以用EXPLAIN或DESCRIB ...
- Loadrunner参数(摘)
一.占有率分析 1. 平均事务响应时间 Average Transaction Response Time 优秀:<2s 良好:2-5s 及格:6-10s 不及格:>10s 2. 每秒点击 ...
- PID算法 旋转倒立摆与平衡车的区别。此贴后边会更新。
我做PID算法的背景和经历:本人之前电子信息科学与技术专业,对控制方向颇感兴趣,刚上大学时听到实验室老师说PID算法,那年在暑假集训准备全国电子设计竞赛,我正在练习做一个以前专科的题目,帆板角度控制系 ...
- Tomcat发布War包或者Maven项目
在tomcat的conf目录下面的server.xml中修改如下: Host name="localhost" appBase="webapps" unpac ...
- elasticsearch启动错误整理
一.elasticsearch错误复现 (一).环境 配置环境 OS:CentOS 7.4 64bit elasticsearch版本: - ip:10.18.43.170 java版本:java - ...
- Python pip包管理器安装第三方库超时解决方案
一.国内镜像安装 使用方法:pip install --index 镜像网站 第三方库名 二.镜像网站 http://pypi.douban.com/simple/ 豆瓣 http://mirrors ...
- 如何实现css渐变圆角边框
最近设计师的风格发生突变,一句话概括就是,能用渐变的地方绝对不用纯色.这不,就整出了一个渐变圆角边框.这渐变好做,圆角好做,渐变圆角也没问题,可是在加个边框还是有点坑的.没办法,看看怎么实现吧 bor ...
- ABAP 金额转换成大写
FUNCTION zzfi_change_amount.*"---------------------------------------------------------------- ...
- 重学计算机组成原理(七)- 程序无法同时在Linux和Windows下运行?
既然程序最终都被变成了一条条机器码去执行,那为什么同一个程序,在同一台计算机上,在Linux下可以运行,而在Windows下却不行呢? 反过来,Windows上的程序在Linux上也是一样不能执行的 ...
- ZooKeeper实现生产-消费者队列
[欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 生产-消费者队列,用于多节点的分布式数据结构,生产和消费数据.生产者创建一个数据对象,并放到队列中:消费者从队列中取出一个数 ...