中国标题不解释

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. 在mysql数据库中关于日期时间字段的处理

    在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...

  2. Windows phone 8 学习笔记(7) 设备

    原文:Windows phone 8 学习笔记(7) 设备 本节主要涉及到 Windows phone 8 手机支持的各类设备,包括相机.设备状态,振动装置等.还有各类感应器,包括磁力计.加速度器和陀 ...

  3. Java 并发编程(三)为线程安全类中加入新的原子操作

    Java 类库中包括很多实用的"基础模块"类.通常,我们应该优先选择重用这些现有的类而不是创建新的类.:重用能减少开发工作量.开发风险(由于现有类都已经通过測试)以及维护成本.有时 ...

  4. PHP socket类

    没事的时候自己封装了一个socket类 功能非常easy和curl功能是一样的 class socketClass{ private $host; private $url; private $err ...

  5. python学习笔记之二:使用字符串

    这里会介绍如何使用字符串格式化其他的值,并了解一下利用字符串的分割,连接,搜索等方法能做些什么. 1.基本字符串操作 所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,取最大值和最小值)对字 ...

  6. Android四个存储数据的SharedPreferences

    除了SQLite外部数据库.SharedPreferences它是一个轻量级的数据存储,其本质是基于XML文件存储key-value键值数据,通过定期用它来存储一些简单的配置信息.它的存储位置/dat ...

  7. 编译gRPC

    编译gRPC 目录 一.概述 二.编译gRPC 三.C#中使用gRPC 四.C++中使用gRPC 无论通过哪种语言调用gRPC,都必须要编译gRPC,因为生成proto访问类时,除了产生标准的数据定义 ...

  8. POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

    题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...

  9. 漫游Kafka介绍章节简介

    原文地址:http://blog.csdn.net/honglei915/article/details/37564521 介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息 ...

  10. iOS开展-Xcode技巧总结(持续更新)

    1. <LLDB调试命令初探> 2. <Xcode LLDB Debug教程> 3. <iOS开发准备篇-(5)Xcode调试技巧_1> 4. <iOS开发准 ...