AC日记——Keywords Search hdu 2222
思路:
ac自动机模板题;
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005 struct TreeNodeType {
int count; TreeNodeType *fail; TreeNodeType *next[]; TreeNodeType()
{
fail=NULL,count=;
for(int i=;i<;i++) next[i]=NULL;
}
};
struct TreeNodeType *root,*que[maxn]; int n; char str[maxn*],word[]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void insert(char *ch)
{
int temp,len=strlen(ch);
TreeNodeType *p=root;
for(int i=;i<len;i++)
{
temp=ch[i]-'a';
if(p->next[temp]==NULL) p->next[temp]=new TreeNodeType;
p=p->next[temp];
}
p->count++;
} void build()
{
int tail=,head=;que[head]=root;
while(head<tail)
{
TreeNodeType *p=que[head++],*temp=NULL;
for(int i=;i<;i++)
{
if(p->next[i]==NULL) continue;
if(p==root) p->next[i]->fail=root;
else
{
temp=p->fail;
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;
}
que[tail++]=p->next[i];
}
}
} int query()
{
int pos,len=strlen(str),res=;TreeNodeType *p=root;
for(int i=;i<len;i++)
{
pos=str[i]-'a';
while(p->next[pos]==NULL&&p!=root) p=p->fail;
if(p->next[pos]!=NULL) p=p->next[pos];else p=root;
TreeNodeType *temp=p;
while(temp!=root&&temp->count!=-) res+=temp->count,temp->count=-,temp=temp->fail;
}
return res;
} int main()
{
int T;
in(T);
while(T--)
{
root=new TreeNodeType;
in(n);for(int i=;i<=n;i++) gets(word),insert(word);
build(),scanf("%s",str),printf("%d\n",query());
}
return ;
}
AC日记——Keywords Search hdu 2222的更多相关文章
- 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自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- Keywords Search HDU - 2222(ac自动机板题。。)
求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...
- AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...
- AC日记——Number Sequence hdu 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- AC日记——统计难题 hdu 1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- AC日记——病毒侵袭 hdu 2896
2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- AC日记——Paint Pearls hdu 5009
Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...
随机推荐
- 【转】 cocos2dx 3.x C++搭建protobuf环境
http://blog.csdn.net/ganpengjin1/article/details/50964961 Cocos2dx 里面在网络游戏通信这一块一般我们都会采用protobuf来进行通信 ...
- js计算当前日期上一个月和下一个月
/** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ funct ...
- ui-grid表格怎么实现内容居中
这次是思想落后了,只关注怎么使用原生的ui-grid样式来实现这一需求,后来发现可以通过此列的cellTemplate来为列指定内容,从而可以使用css调整样式. ps:其实有时候换种思路,豁然开朗. ...
- Redis集群操作手册
一.原始集群(6节点 3主3从): (1)启动集群: [root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-clust ...
- JSP动作标识
jsp中include有两种形式: include指令:<%@ include file=""%> include动作:<jsp:include page=&qu ...
- sql 先查出已知的数据或者需要的数据再筛选
sql 先查出已知的数据或者需要的数据再筛选
- P2483 【模板】k短路([SDOI2010]魔法猪学院)
题目背景 感谢@kczno1 @X_o_r 提供hack数据 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界 ...
- BZOJ 3319 黑白树 并查集+线段树
这这这这这这什么毒瘤题!!!!!!!!!!!!!!!!!!!!!!!!!!!! 卡LCT(优秀的LCT由于是均摊本身就带着2,3的常数在,而且这道题对于LCT标记十分难维护,又得乘上4,5然后就炸了) ...
- JavaScript使用数组拼接字符串性能如何?
传统上,字符串连接一直是js中性能最低的操作之一. view source print? 1 var text="Hello"; 2 text+=" World!&q ...
- CSS中的text-shadow。
text-shadow(文字投影),box-shadow(容器投影),border-radius(圆角)这三个属性估计以后用的比较多,记录 一下.目前不支持IE系列(不过可以使用其他方法实现,下文有详 ...