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的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
随机推荐
- _bzoj1500 [NOI2005]维修数列【真·Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...
- Mondriaan's Dream POJ - 2411
Mondriaan's Dream POJ - 2411 可以用状压dp,但是要打一下表.暴力枚举行.这一行的状态.上一行的状态,判断如果上一行的状态能转移到这一行的状态就转移. 状态定义:ans[i ...
- 持有对方的引用&&内部类
现在来做个很简单的东西,就是做一个做加法的图形界面 然后现在先是一个不用持有对方引用的写法: import java.awt.*; import java.awt.event.*; public cl ...
- 学习笔记 第十五章 JavaScript基础
第15章 JavaScript基础 [学习重点] 了解JavaScript基础知识 熟悉常量和变量 能够使用表达式和运算符 正确使用语句 能够掌握数据类型和转换的基本方法 正确使用函数.对象.数组 ...
- 学习笔记 第九章 使用CSS美化表格
第9章 使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...
- Android学习笔记(十三) Handler
可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...
- struts2 <allowed-methods > 标签配置
1.在struts2 2.5版本中添加了对方法访问的权限,如果没有被添加到<allow-method> 方法的标签,将会报一下错误 5:05:18.078 [http-apr-8020 ...
- iOS Programming Controlling Animations 动画
iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- 自制无线共享工具C++源代码及创建过程
// wire.cpp : 定义控制台应用程序的入口点.// #include <iostream>#include <string.h>using namespace std ...