病毒侵袭持续中

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. ACM_求N^N的最高位数

    Leftmost Digit Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个正整数N,你应该输出N ^ N的最左边的数字 ...

  2. ssrs 2016, mobile report error: The report may be misconfigured, the data may not be available, or the server version may be unsupported.

    使用多账户配置ssrs mobile report 权限后,使用客户端显示: 使用web 查阅,显示: 遇到这种情况,可能是由于,report引用了 数据集文件夹中的数据集,请记得把数据集文件夹上为该 ...

  3. sdut1933WHUgirls(dp)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1933 矩形的dp一般挺类似  大的矩形都是由小 ...

  4. Repeater 的使用

    <HeaderTemplate></HeaderTemplate>  头模板——在加载开始执行一遍 <FooterTemplate></FooterTempl ...

  5. js点击修改按钮后修改

    <button id="click">改变内容</button> <div id="t">要改变的内容</div> ...

  6. 使用iconfont管理项目中的字体图标

    先来说说字体图标的好处: 很容易任意地缩放: 很容易地改变颜色: 很容易地产生阴影: 可以拥有透明效果: 一般来说,有先进的浏览器支持: 可以使用CSS来装饰(可以得到CSS很好支持): 可以快速转化 ...

  7. orcale 数据库的一些知识

    最近学了一些Oracle数据库的知识,我想自己整理一下,以后也方便自己查阅的. orcale 数据库登录(tiger) 1. sql plus 登录 用户名: sys 口令: 主机字符串:orcl a ...

  8. 仿陌陌的ios客户端+服务端源码

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...

  9. 迅为嵌入式开发板iTOP-6818开发板八核Cortex-A53架构,满足各种产品需求

    性价比更高 内存:1G(可选2G);存储:16G;4418:四核 Cortex-A9;6818:八核Cortex-A53. 功能更强 板载4G(全网通),GPS,WIFI,千兆以太网,重力加速度计等, ...

  10. 习水医院12C RAC 数据库安装文档

        环境介绍 OS: Oracle Enterprise Linux 6.4 (For RAC Nodes) DB: GI and Database 12.1.0.2 所需介质 p17694377 ...