HDU3065:病毒侵袭持续中——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3065
————————————————————————————
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
Hit:
题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。
——————————————————————————
AC自动机裸题,没啥好说的。
注意是多组数据,而且数据中可能有空格算字符注意下就行了。
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int N=;
const int M=;
struct trie{
int ed;
int a[];
int fail;
void clear(){
fail=ed=;
memset(a,,sizeof(a));
return;
}
}tree[];
int cnt=;
char s[N][M];
char s0[];
inline void insert(int k){
int now=;
int len=strlen(s[k]);
for(int i=;i<len;i++){
int t=s[k][i]-'A';
if(!tree[now].a[t]){
cnt++;
tree[now].a[t]=cnt;
tree[cnt].clear();
}
now=tree[now].a[t];
}
tree[now].ed=k;
return;
}
void getfail(){
queue<int>q;
tree[].fail=;
for(int i=;i<;i++){
if(tree[].a[i]){
tree[tree[].a[i]].fail=;
q.push(tree[].a[i]);
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<;i++){
if(tree[u].a[i]){
tree[tree[u].a[i]].fail=tree[tree[u].fail].a[i];
q.push(tree[u].a[i]);
}else{
tree[u].a[i]=tree[tree[u].fail].a[i];
}
}
}
return;
}
int vis[N];
void check(){
memset(vis,,sizeof(vis));
int now=;
int len=strlen(s0);
for(int i=;i<len;i++){
int t=s0[i]-'A';
if(t<||t>){
now=;
continue;
}
now=tree[now].a[t];
for(int j=now;j;j=tree[j].fail){
if(tree[j].ed){
vis[tree[j].ed]++;
}
}
}
return;
}
int main(){
int n;
while(cin>>n){
if(!n)break;
tree[].clear();
for(int i=;i<=n;i++){
cin>>s[i];
insert(i);
}
getfail();
getchar();
gets(s0);
check();
for(int i=;i<=n;i++){
if(vis[i]){
cout<<s[i];
printf(": %d\n",vis[i]);
}
}
}
return ;
}
HDU3065:病毒侵袭持续中——题解的更多相关文章
- HDU3065 病毒侵袭持续中 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu3065 病毒侵袭持续中
题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...
- 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 Sub ...
- HDU-3065 病毒侵袭持续中 AC自动机又是一板子!
病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...
- [hdu3065]病毒侵袭持续中(AC自动机)
题意:给出多种病毒的号码和特征码,计算在某串中各病毒匹配的次数. 解题关键:AC自动机模板题,多组输入坑人. #include<bits/stdc++.h> using namespace ...
- HDU-3065 病毒侵袭持续中 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-3065 题意 跟上一道题是几乎一模一样,这次是统计关键词的出现次数 一个相当坑的地方,注意多组样例 思路 套模版 改in ...
- hdu3065病毒侵袭持续中
链接 上一篇的姊妹篇 没啥好说的 套模板 #include <iostream> #include<cstdio> #include<cstring> #inclu ...
随机推荐
- Visual Studio 2015 Test Explorer does not show anything
Problem After install Visual Studio 2015 community and NUnit Test Adapter, I cannot find test cases ...
- tomcat6升级到7时400问题,以及url带有汉字时出错。
tomcat6升级到7时400问题: 在文件catalina.properties后加入tomcat.util.http.parser.HttpParser.requestTargetAllow=|. ...
- uvaoj 1081510815 - Andy's First Dictionary(set应用)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...
- 【SpringCloud】第六篇: 分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- Spring 配置请求过滤器,编码格式设为UTF-8,避免中文乱码
<!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> <filter-name>springUtf8Encoding</fi ...
- 【text】 文本组件说明
text文本组件:在小程序里除了文本节点以外的其他节点都无法长按选中. 原型: <text selectable="[Boolean]" space="[ensp ...
- yii的学习笔记 基本结构 自用
Yii 学习笔记 W:YII是什么? Q:Yii 是一个基于组件的高性能 PHP 框架,用于快速开发大型 Web 应用.它使Web开发中的 可复用度最大化,可以显著提高你的Web应用开发速度.Yii ...
- [leetcode-693-Binary Number with Alternating Bits]
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- NSValue的valueWithBytes:objCType:方法
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type; NSValue的valueWithBytes: ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...