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的更多相关文章

  1. Keywords Search HDU - 2222 ( ac自动机)模版题

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  2. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  3. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  4. Keywords Search HDU - 2222(ac自动机板题。。)

    求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...

  5. AC自动机---Keywords Search

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. AC日记——统计难题 hdu 1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  9. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

随机推荐

  1. div加了float后 四个特性

    1.宽度变成0 2.左漂浮 或者右漂浮 3.后面的标签占据原来的位置 4对前面的div没有影响 他会浮动到前面div下面

  2. 背景图片移动插件MyFloatingBg(浮动背景图效果,可让背景随着页面的滚动而滚动)

    MyFloatingBg这插件可以帮助你在网页上加入可移动背景Background.你可以用于整个文件的背景,或是某几个banner的背景. 它可支持简单的animation效果,你不用去做一个fla ...

  3. 奇异值分解(SVD)小结

    SVD(奇异值分解)真的是一个神奇的东西,这里就写个小结. 其实原理并不是那么难理解. 它在数据去噪方面和降维上有特殊作用,也与PCA有很大的联系. 首先我们先回顾一下 EVD,特征值分解,可以对SV ...

  4. bootstrap-table 回显选中行,行样式

    { filed:'status', checkbox:true, formatter:function(value,row,index){ if (row.status == 1) //根据行里字段判 ...

  5. [codeforces] 633C Spy Syndrome 2

    原题 Trie树+dp 首先,我们可以简单的想到一种dp方式,就是如果这一段可以匹配并且可以与前一段接上,那么更新dp[i]为当前字符串的编号,然后倒推就可以得到答案. 但是,显然我们不能O(m)比较 ...

  6. 安徽师大附中%你赛day9 T3 贵 解题报告

    贵 问题描述 苟先生的狼狗大军没有追上富先生, 所以他把它们都解雇了, 决定去雇佣一些更好的狗, 不过狗可是很贵的.苟先生有 \(w\) 元钱, 有 \(n\) 条狗可以雇佣, 第 \(i\) 条狗有 ...

  7. 牛客多校对抗第6场 A Singing Contest

    [20分]标题:A.Singing Contest | 时间限制:1秒 | 内存限制:256MJigglypuff is holding a singing contest. There are 2n ...

  8. java实现极简的LRU算法

    import java.util.LinkedHashMap;import java.util.Map; /** * LRU (Least Recently Used)  */public class ...

  9. 迅雷Bolt图像拉伸不清晰的解决办法

    迅雷Bolt库中的图像拉伸的效果锯齿比较严重,常见的导致锯齿的情况: 1.在使用ImageObject时,drawmode为1拉伸模式下: 2.使用Bitmap类的Stretch函数拉伸图像: 虽然I ...

  10. MVC前台获取ViewData的数组中的值

    查了一上午资料,找到了一种比较有效的方法 后台控制器:public ActionResult Index() { List<string> colors = new List<str ...