hdu3065 病毒侵袭持续中【AC自动机】
病毒侵袭持续中
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19553 Accepted Submission(s): 6409
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
AA
BB
CC
ooxxCC%dAAAoen....END
CC: 1
Hit:
题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。
题意:
还是n个病毒,现在是1个文本串。统计模式串在文本串中出现的次数。
思路:
这个不是统计个数,就不需要vis了。同样要注意文本串中有可能有除了字母以外的字符,son还是要开了130
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int m, n;
const int maxn = ;
const int maxlen = 2e6 + ; struct virus{
char str[];
int id;
}vir[maxn];
struct tree{
int fail;
int son[];
int ed;
//bool vis;
}AC[maxlen];
int tot = ;
char s[maxlen]; void build(char s[], int id)
{
int len = strlen(s);
int now = ;
for(int i = ; i < len; i++){
if(AC[now].son[s[i]] == ){
AC[now].son[s[i]] = ++tot;
}
now = AC[now].son[s[i]];
}
AC[now].ed = id;
} void get_fail()
{
queue<int>que;
for(int i = ; i < ; i++){
if(AC[].son[i] != ){
AC[AC[].son[i]].fail = ;
que.push(AC[].son[i]);
}
}
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = ; i < ; i++){
if(AC[u].son[i] != ){
AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
que.push(AC[u].son[i]);
}
else{
AC[u].son[i] = AC[AC[u].fail].son[i];
}
}
}
} int cnt[maxn];
void AC_query(char s[])
{
/*for(int i = 0; i <= tot; i++){
AC[i].vis = false;
}*/
int len = strlen(s);
int now = ;
for(int i = ; i < len; i++){
now = AC[now].son[s[i]];
for(int t = now; t; t = AC[t].fail){
if(AC[t].ed != ){
cnt[vir[AC[t].ed].id]++;
//AC[t].vis = true;
}
}
}
} int main()
{
while(scanf("%d", &n) != EOF){
for(int i = ; i <= tot; i++){
AC[i].fail = ;
//AC[i].vis = false;
AC[i].ed = ;
for(int j = ; j < ; j++){
AC[i].son[j] = ;
}
}
tot = ;
for(int i = ; i <= n; i++){
cnt[i] = ;
scanf("%s", vir[i].str);
vir[i].id = i;
build(vir[i].str, vir[i].id);
}
AC[].fail = ;
get_fail();
getchar();
gets(s);
AC_query(s);
for(int i = ; i <= n; i++){
if(cnt[i]){
printf("%s: %d\n", vir[i].str, cnt[vir[i].id]);
}
}
}
return ;
}
hdu3065 病毒侵袭持续中【AC自动机】的更多相关文章
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- HDU3065 病毒侵袭持续中 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu----(3065)病毒侵袭持续中(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自动机)
题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...
- hdoj 3065 病毒侵袭持续中(AC自动机)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...
- hdu3065 病毒侵袭持续中
题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...
- HDU-3065 病毒侵袭持续中 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-3065 题意 跟上一道题是几乎一模一样,这次是统计关键词的出现次数 一个相当坑的地方,注意多组样例 思路 套模版 改in ...
随机推荐
- hibernate 之 sql查询
如果用hibernate执行原生sql进行数据查询可以调用 SQLQuery query = getSession().createSQLQuery(sql); 然后再执行 query.list() ...
- [原]unity3d GLSL无法pc上显示
pc编写GLSL: Shader "Custom/GLSL" { Properties { _MainTex ("Base (RGB)", ...
- LED驱动程序分析
混杂设备 LED驱动程序分析 /******************************* * *杂项设备驱动:miscdevice *majior=10; * * *************** ...
- c# windows服务
参考:https://www.cnblogs.com/knowledgesea/p/3616127.html 序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而 ...
- lua 按拉分析与合成
-- 将数值分解成bytes_table local function decompose_byte(data) if not data then return data end local tb = ...
- Java最快的maven仓库地址,国内Maven地址,超快的Maven地址
Java最快的maven地址,国内Maven地址,超快的Maven地址 >>>>>>>>>>>>>>>> ...
- PostgreSQL存储过程(1)-基于SQL的存储过程
什么是SQL函数? SQL函数包体是一些可执行的SQL语言.同时包含1条以上的查询,但是函数只返回最后一个查询(必须是SELECT)的结果. 除非SQL函数声明为返回void,否则最后一条语句必须是S ...
- Linux-selinux
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- vc11(vs2012)下编译php源码
需要原料: vs2012.php源码 1.本机的mingw没搞定,参考网上文章尝试vs2012编译,借助vs2012自带的命令行工具: 需要去bison官网下载bison.exe放在“c:/windo ...
- VC下遍历文件夹中的所有文件的几种方法
一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...