BNUOJ 7178 病毒侵袭持续中
病毒侵袭持续中
This problem will be judged on HDU. Original ID: 3065
64-bit integer IO format: %I64d Java class name: Main
Input
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
Output
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
Sample Input
3
AA
BB
CC
ooxxCC%dAAAoen....END
Sample Output
AA: 2
CC: 1
Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。
Source
#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 病毒侵袭持续中的更多相关文章
- hdu3065 病毒侵袭持续中
题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...
- AC自动机---病毒侵袭持续中
HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...
- HDU 3065 病毒侵袭持续中
HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu----(3065)病毒侵袭持续中(AC自动机)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 【HDU3065】 病毒侵袭持续中(AC自动机)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 3065 病毒侵袭持续中【AC自动机】
<题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...
- hdu3065 病毒侵袭持续中【AC自动机】
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 3065病毒侵袭持续中
病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 3065 病毒侵袭持续中 (AC自动机)
题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
随机推荐
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
- 16-1 WEB存储基本操作
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Jquery插件jqprint-0.3.js实现打印
1.首先引用Jquery和jqprint-0.3.js(依赖于Jquery的) <script language="javascript" src="jquery- ...
- 设计模式 -- Abstract Factory 抽象工厂
1.常规的对象创建方法 //创建一个Road对象 Road road=new Road(); new的问题:实现依赖,不能应对“具体实例化类型”额变化. 解决思想: 封装变化点--哪里变化,封装哪里( ...
- robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!
真心不容易呀!开源软件搭建挺麻烦的,各种组件未必要使用最新的版本:有些最新版本反而不兼容.需要仔细看官方说明书来进行搭建(官方网站都是英文),所以闹得重新安装了几次. 先上测试用例通过的图:
- 手动配置wamp环境(1)--apache安装与基本操作
Apache服务器简介: Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. 安装apac ...
- Laravel Mix编译前端资源
目前项目是使用的vue+laravel来写的,其中laravel和vue分别放了一个目录,但是这样有个问题,那就是vue需要经常更新,不然运行项目会经常出现各种问题,这里就看了看laravel的文档, ...
- E. Wrong Answer
E. Wrong Answer time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 浅谈JS之text/javascript和application/javascript
问题描述: JS在IE8以下浏览器运行异常 代码: <script>标签是这样子写的: <script type="application/javascript" ...
- 00C#
C# C#(读作“See Sharp”)是一种简单.现代.面向对象且类型安全的编程语言.C# 起源于 C 语言家族,因此,对于 C.C++ 和 Java 程序员,可以很快熟悉这种新的语言.C# 已经分 ...