病毒侵袭持续中 HDU
来源:互联网 发布:人生老鼠知乎 编辑:程序博客网 时间:2024/04/28 19:00
Problem Description
小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中推测。
思路:和病毒侵袭 HDU - 2896 这题差不多,只是多了一个计数。
注意有多组数据
代码如下
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<string>#include<map>#define LL long long using namespace std;const int maxn=128;char str1[1010][100]; char str2[2000005];struct Trie{ int next[1010*50][maxn]; int fail[1010*50],end[1010*50]; int root,L; int newnode() //初始化 { for(int i=0;i<128;i++) next[L][i]=-1; end[L++]=-1; return L-1; } void init()//初始化 { L=0; root=newnode(); } void insert(char s[],int id)//构造树trie { int len=strlen(s); int now=root; for(int i=0;i<len;i++) { if(next[now][s[i]]==-1) next[now][s[i]]=newnode(); now=next[now][s[i]]; } end[now]=id;//记录单词的id } void build()//构造失败指针 { queue<int>Q; fail[root]=root; for(int i=0;i<128;i++) if(next[root][i]==-1) next[root][i]=root; else { fail[next[root][i]]=root; Q.push(next[root][i]); } while(!Q.empty()) { int now=Q.front(); Q.pop(); for(int i=0;i<128;i++) if(next[now][i]==-1) next[now][i]=next[fail[now]][i]; else { fail[next[now][i]]=next[fail[now]][i]; Q.push(next[now][i]); } } } int vis[1010];//记录出现的次数 void query(char buf[],int n)//查询 { int len=strlen(buf); int now=root; memset(vis,0,sizeof(vis)); bool flag=false; for(int i=0;i<len;i++) { now = next[now][buf[i]]; int temp = now; while(temp != root) { if(end[temp] != -1) { vis[end[temp]]++; flag = true; } temp = fail[temp]; } } if(flag) for(int i = 1;i <= n;i++) if(vis[i]) printf("%s: %d\n",str1[i],vis[i]); return ; } };Trie ac;int main(){ int n,m; while(scanf("%d",&n) != EOF) { ac.init(); for(int i = 1;i <= n;i++) { scanf("%s",str1[i]); ac.insert(str1[i],i); } ac.build(); scanf("%s",str2); ac.query(str2,n); } return 0;}
0 0
- 病毒侵袭持续中 HDU
- HDU 2896 病毒侵袭 && HDU 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- HDU 3065 病毒侵袭持续中
- Hdu 3065 病毒侵袭持续中
- HDU 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中 ac_automaton
- HDU 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- HDU 3065 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- HDU 3065 病毒侵袭持续中
- HDU - 3065 病毒侵袭持续中
- hdu 3065 - 病毒侵袭持续中
- hdu 3065 病毒侵袭持续中
- 基于线性回归预测房价
- ScrollView 组件原生事件onScroll
- ActiveMQ-5.9.0 安装和启动
- React-router4
- 剑指offer-构建乘积数组-php
- 病毒侵袭持续中 HDU
- 文章标题
- ReactNative might be related to https://github.com/facebook/react-native/issues/4968
- 把视频中的一小段做成GIF动图
- EncodingFilter
- Eclipse背景颜色修改
- spring IOC原理
- OpenCV/OpenCL/OpenGL区别
- 计算机网络体系结构