中国标题不解释

Sample Input
3
AA
BB
CC
ooxxCC%dAAAoen....END
 
Sample Output
AA: 2
CC: 1

输出病毒出现的次数!

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
const int kind = 26;
const int M = 1005;
struct node
{
node *fail;
node *next[kind];
int count;
node()
{
fail = NULL;
count = 0;
memset(next,0,sizeof(next));
}
}*q[50*M];
char keyword[1005][55],str[2000010];
int head,tail;
void insert(char *str,node *root,int num)
{
node *p=root;
int i=0,index;
while(str[i])
{
index = str[i] - 'A';
if(p->next[index]==NULL)
p->next[index] = new node();
p = p->next[index];
i++;
}
p->count=num;
} void build_ac(node *root)
{
int i;
root->fail=NULL;
q[head++]=root;
while(head!=tail)
{
node *temp = q[tail++];
node *p=NULL;
for(i=0;i<kind;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)
temp->next[i]->fail=root;//失败指针指向root
else
{
p = temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;
}
q[head++]=temp->next[i];
}
}
}
}
int vis[1005];
void query(char *str,node *root)
{
int i=0,cnt=0,index,len=strlen(str);
node *p=root;
while(str[i])
{
if(str[i]<'A'||str[i]>'Z')
{
p=root;
i++;
continue; }
index = str[i]-'A';
while(p->next[index]==NULL&&p!=root)
p = p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp = p;
while(temp!=root)
{
vis[temp->count]++;
temp=temp->fail;
}
i++;
}
} int main()
{
int n,m;
while(~scanf("%d",&n))
{
head=tail=0;
node *root = new node();
getchar();
for(int i=1;i<=n;i++)
{
gets(keyword[i]);
insert(keyword[i],root,i);
}
build_ac(root);
scanf("%s",str);
memset(vis,0,sizeof(vis));
query(str,root);
for(int i=1;i<=n;i++)
if(vis[i])
printf("%s: %d\n",keyword[i],vis[i]);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 3065 病毒在继续 (AC自己主动机)的更多相关文章

  1. 【HDU】病毒侵袭(AC自己主动机模板题)

    AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...

  2. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  3. 【HDU 5384】Danganronpa(AC自己主动机)

    看官方题解貌似就是个自己主动机裸题 比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢.. 存下模板吧 #include<cstdio> #include&l ...

  4. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  5. HDU 2222 Keywords Search(AC自己主动机模板题)

    题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...

  6. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  7. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

  8. HDU 3065 病毒侵袭持续中

    HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  10. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

随机推荐

  1. art patchoat

    Add patchoat tool to Art. Add a new executable called patchoat to art. This tool takes alreadycompil ...

  2. 伪教练技术培训之殇-2013年9月江西IDC拓行榜与综述

    纠集几个人,然后培训所谓的教练技术培训. 培训的人一期又一期的参与,国学.佛学.超能量,无所不用其极,然后就是疯狂的拿人头,邀请朋友加盟. 有甚者还披上“科技”的外衣,用“水知道答案”这种早被公知指出 ...

  3. Linux下忘记rootpassword怎么办?

    1.首先确认server出于安全的状态.也就是没有人可以随意地连接MySQL数据库. 由于在又一次设置MySQL的rootpassword的期间,MySQL数据库全然出于没有password保护的  ...

  4. Xcode6在10.9.4上面crash解决

    具体请看我的evernote 这里: 在10.9.4系统上面直接安装xcode6的beta3.和平时一样, 1.将beta3拖拽到application文件夹中. 2.等待copy完毕,执行xcode ...

  5. 推测的手机型号和cpu模型

    <span style="font-size:18px;">推断手机型号:</span> <span style="font-size:18 ...

  6. centos下ant的安装

    1.创建一个文件夹,用于安装ant.我们这里临时在/usr文件夹下创建ant文件夹. 2.下载 cd /usr/ant进入该文件夹,使用wget  ant的下载地址  下载ant到当前文件夹下.附an ...

  7. 快捷找jar包方式

    通过例如以下站点查找jar包: 1.http://www.java2s.com/Code/Jar/o/Downloadorgapacheservicemixbundlescommonslang246j ...

  8. 建立qemu桥接的网络连接

    转载请注明出处谢谢:http://www.openext.org/2014/07/qemu-kvm-bridge-00     安装桥接工具:sudo apt-get install bridge-u ...

  9. ecshop购物流程中去掉email邮箱

    首先打开includes\lib_order.php,在第1688行左右找到并删除 !empty($consignee['email']) && 接着打开js\shopping_flo ...

  10. windows phone 页面导航(6)

    原文:windows phone 页面导航(6) 页面导航的例子我们使用的是两个页面,从第一个页面(MainPage)导航到第二个页面(SecondPage),然后可以从第二个页面导航到第一个页面 , ...