病毒侵袭持续中

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3065
64-bit integer IO format: %I64d      Java class name: Main

 
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?

 

Input

第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。

 

Output

按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。

 

Sample Input

3
AA
BB
CC
ooxxCC%dAAAoen....END

Sample Output

AA: 2
CC: 1
Hint

Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。

Source

 
 
解题:AC自动机。。。。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
struct trie{
int wd[],fail,index;
void init(){
memset(wd,,sizeof(wd));
fail = index = ;
}
};
trie dic[];
int tot,n,cnt[];
char word[][];
char web[];
void insertWd(int root,int id,char *s){
for(int i = ; s[i]; i++){
int k = s[i]-'A';
if(!dic[root].wd[k])
dic[root].wd[k] = tot++;
root = dic[root].wd[k];
}
dic[root].index = id;
}
void build(int root){
queue<int>q;
q.push(root);
int u,v,i,j;
while(!q.empty()){
u = q.front();
q.pop();
for(i = ; i < ; i++){
if(!dic[u].wd[i]) continue;
if(!u) dic[dic[u].wd[i]].fail = ;
else{
v = dic[u].fail;
while(v && !dic[v].wd[i]) v = dic[v].fail;
if(dic[v].wd[i]) dic[dic[u].wd[i]].fail = dic[v].wd[i];
else dic[dic[u].wd[i]].fail = ;
}
q.push(dic[u].wd[i]);
}
}
}
void query(int root,char *s){
for(int i = ; s[i]; i++){
int k = s[i]-'A';
if(k < || k > ) {root = ;continue;}
while(root && !dic[root].wd[k]) root = dic[root].fail;
root = dic[root].wd[k];
if(root){
int v = root;
while(v && dic[v].index){
cnt[dic[v].index]++;
v = dic[v].fail;
}
}
}
}
int main() {
int i,root;
while(~scanf("%d",&n)){
tot = ;
root = ;
for(i = ; i < ; i++)
dic[i].init();
memset(cnt,,sizeof(cnt));
for(i = ; i <= n; i++){
scanf("%s",word[i]);
insertWd(root,i,word[i]);
}
build(root);
scanf("%s",web);
query(root,web);
for(i = ; i <= n; i++){
if(cnt[i]) printf("%s: %d\n",word[i],cnt[i]);
}
}
return ;
}
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int cnt[];
char str[][];
struct Trie{
int ch[maxn][],fail[maxn],id[maxn],tot;
int newnode(){
memset(ch[tot],,sizeof ch[tot]);
id[tot] = fail[tot] = ;
return tot++;
}
void init(){
tot = ;
newnode();
}
void insert(char *str,int index,int root = ){
for(int i = ; str[i]; ++i){
if(!ch[root][str[i]-'A']) ch[root][str[i]-'A'] = newnode();
root = ch[root][str[i]-'A'];
}
id[root] = index;
}
void build(int root = ){
queue<int>q;
for(int i = ; i < ; ++i)
if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()){
root = q.front();
q.pop();
for(int i = ; i < ; ++i){
if(ch[root][i]){
fail[ch[root][i]] = ch[fail[root]][i];
q.push(ch[root][i]);
}else ch[root][i] = ch[fail[root]][i];
}
}
}
void query(char *str,int root = ){
for(int i = ; str[i]; ++i){
int k = str[i] - 'A';
if(k >= || k < ) {
root = ;
continue;
}
int x = root = ch[root][k];
while(x){
cnt[id[x]]++;
x = fail[x];
}
}
}
}ac;
char ss[];
int main(){
int n;
while(~scanf("%d",&n)){
ac.init();
for(int i = ; i <= n; ++i){
scanf("%s",str[i]);
ac.insert(str[i],i);
}
ac.build();
memset(cnt,,sizeof cnt);
scanf("%s",ss);
ac.query(ss);
for(int i = ; i <= n; ++i)
if(cnt[i]) printf("%s: %d\n",str[i],cnt[i]);
}
return ;
}

BNUOJ 7178 病毒侵袭持续中的更多相关文章

  1. hdu3065 病毒侵袭持续中

    题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...

  2. AC自动机---病毒侵袭持续中

    HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...

  3. HDU 3065 病毒侵袭持续中

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

  4. hdu----(3065)病毒侵袭持续中(AC自动机)

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

  5. 【HDU3065】 病毒侵袭持续中(AC自动机)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. hdu 3065 病毒侵袭持续中【AC自动机】

    <题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...

  7. hdu3065 病毒侵袭持续中【AC自动机】

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

  8. hdu 3065病毒侵袭持续中

    病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. HDU 3065 病毒侵袭持续中 (AC自动机)

    题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...

随机推荐

  1. 51Nod 1315 合法整数集

    1315 合法整数集 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一个整数集合S是合法的,指S的任意子集subS有Fu ...

  2. Errors running builder 'JavaScript Validator'错误处理

    MyEclipse2014编辑代码时,只要保存就会报出如下错误信息: Errors occurred during the build. Errors running builder 'JavaScr ...

  3. Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏

    下面是用Matlab实现的Butterworth高通.低通滤波器. clc;clear all;close all; I=imread('cameraman.tif'); subplot(3,2,1) ...

  4. HBase Region Assign流程详解

    Hbase是kv存储,但是逻辑上我们可以把存储在hbase上的kv数据当成表,rowkey可以认为是表的主键.为了便于分布式操作,hbase会把表横向切分成一块一块的数据,而每块就是一个Region. ...

  5. iOS捷径(Workflow 2.0)拓展

    前言 iOS12 捷径(Workflow 2.0)入门 iOS12 捷径(Workflow 2.0)进阶 iOS12捷径(Workflow 2.0)实例大全 注:本文主要介绍如何获取URL Schem ...

  6. Android EventBus3.0详解

    修改日志 -- 添加索引部分得细节,添加kotlin的支持方式 https://www.jianshu.com/p/31e3528ca7e5

  7. 【PostgreSQL-9.6.3】表空间

    在PostgreSQL中,表空间实际上是为表指定一个存储目录,这样方便我们把不同的表放在不同的存储介质或者文件系统中.在创建数据库.表.索引时都可以指定表空间. 1. 创建表空间 --表空间目录必须是 ...

  8. getDate() 各种时间格式

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE() ...

  9. ZooKeeper系列(三)

    前面虽然配置了集群模式的Zookeeper,但是为了方面学建议在伪分布式模式的Zookeeper学习Zookeeper的shell命令. 一.Zookeeper的四字命令 Zookeeper支持某些特 ...

  10. BEGIN - 开始一个事务块

    SYNOPSIS BEGIN [ WORK | TRANSACTION ] DESCRIPTION 描述 BEGIN 初始化一个事务块, 也就是说所有 BEGIN 命令后的用户语句都将在一个事务里面执 ...