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的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
随机推荐
- _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...
- _bzoj1061 [Noi2008]志愿者招募【最小费用最大流】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1061 尽管不是mcmf的裸题,但还是保存一下模版叭~ 很好的一道建模的题,把变量间的加加减减 ...
- [ZPG TEST 108] Antimonotonicity【贪心】
T2:Antimonotonicity (Antimonotonicity.pas/in/out 128M 1s) 给你1-N的一个排列,数列中的数字互不相等,要求找出最长的子序列a,满足a1> ...
- 暴力 BestCoder Round #46 1001 YJC tricks time
题目传送门 /* 暴力:模拟枚举每一个时间的度数 详细解释:http://blog.csdn.net/enjoying_science/article/details/46759085 期末考结束第一 ...
- Android开机自启动
1.原理 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED. ...
- PHP 官方说明
http://php.net/manual/en/mysqli.affected-rows.php The above examples will output: Affected rows (INS ...
- UML 顺序图(转载)
顺序图精确表达用户与系统的复杂交互过程. 顺序图用于描述进出系统的信息流. 顺序图与协作图是同构的,可以互相转换!!! 顺序图:着重体现对象间消息传递的时间顺序.顺序图允许直观的表示出对象的生存期,生 ...
- Spring Mvc相关随笔
web.xml部分 1.欢迎界面 <welcome-file-list> <welcome-file>/views/login.jsp</welcome-file> ...
- 将php中session存入redis中
PHP 的会话默认是以文件的形式存在的,可以配置到 Redis 中,即提高了访问速度,又能很好地实现会话共享! 配置方式如下: 方法一:修改 php.ini 的设置 session.save_hand ...
- 正则表达式,匹配查找函数(preg_match_all)flags参数对比
格式: int preg_match_all ( string pattern, string subject, array matches [, int flags] ) 参数 flags 选项有以 ...