hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/**
题目:hdu2896 病毒侵袭
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896
题意:N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同),
M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
题目保证每个待匹配串中最多有三个模式串。
思路:ac自动机做法,字符为可见字符,那么直接就是他们的ascii值作为每一个字符的标志。最多128;
由于不超过三个,所以找到3个就可以return ;节约时间。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/ #include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
vector<int> ans;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
//int c = idx(s[i]);
int c = s[i];
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = T[i];
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
if(ans.size()==) return ;
}
} void print(int j)
{
if(j){
ans.push_back(val[j]);
if(ans.size()==) return ;
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
int main()
{
int n, m;
while(scanf("%d",&n)==)
{
ac.clear();
for(int i = ; i <= n; i++){
scanf("%s",s);
ac.insert(s,i);
}
ac.getFail();
scanf("%d",&m);
int cnt = ;
for(int i= ; i <= m; i++){
ans.clear();
scanf("%s",s);
ac.find(s);
if(ans.size()!=){
cnt++;
printf("web %d:",i);
sort(ans.begin(),ans.end());
for(int j = ; j < (int)ans.size(); j++){
printf(" %d",ans[j]);
}
printf("\n");
}
}
printf("total: %d\n",cnt);
}
return ;
} /*
3
aaa
bbb
ccc
2
aaabbbccc
bbaacc
*/
hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,的更多相关文章
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- HDU2896 病毒侵袭 —— AC自动机
题目链接:https://vjudge.net/problem/HDU-2896 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- hdu2896 病毒侵袭 ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...
- hdu2896病毒侵袭(ac自动机)
链接 ac自动机的模板题 说2个注意的地方 一是题目说明包含所有ASCII字符,可以开到0-127 包含空格 题目会输入多个源串,在加完当前的val值时,不应清0,可以开个标记数组. #include ...
- hdu 2896 病毒侵袭 AC自动机 基础题
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU2896 病毒侵袭 AC自动机模板
各种MLE,这模板感觉有问题,next数组开128也会MLE,实际上可见字符为编号32~126,只用开100就行. #include <iostream> #include <cst ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- HDUOJ----2647Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Python websocket
一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...
- 网站跳转到cgi-sys/defaultwebpage.cgi的原因和解决方式
cpanel遇到这种问题,看了这篇文章老鹰主机域名解析A记录教程–关于cgi-sys/defaultwebpage.cgi后,尝试后 首先ping 域名,结果如下 看到没有ping结果 ...
- 转:C#实现office文档转换为PDF或xps的一些方法
代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...
- Linux进程冻结技术
1 什么是进程冻结 进程冻结技术(freezing of tasks)是指在系统hibernate或者suspend的时候,将用户进程和部分内核线程置于“可控”的暂停状态. 2 为什么需要冻结技术 假 ...
- PowerDesigner列名作为注释
Tools -> Execute Commands -> Edit/Run Script 执行以下脚本: Option Explicit ValidationMode = True Int ...
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
在上一篇文章Android IPC机制(二)用Messenger进行进程间通信中我们介绍了使用Messenger来进行进程间通信的方法.可是我们能发现Messenger是以串行的方式来处理client ...
- [转]VTH changes in DC from Hspice
Hello, everyone. I’d like to know the threshold of the MOS transistor. And I found the “.print vth() ...
- 解决maven构建webapp index.jsp报错问题
今天早上想用maven 构建一个webapp 然后index.jsp华华丽丽的报错了 当时我的心情是一万头草泥马奔过啊,为啥你给我创建的webapp 还会报错啊!!!!!! 然后百度了一下,各种说少 ...
- unity camera aspect
camera的aspect默认是与screen保持一致,可以通过脚本将其设为指定值,如下: using UnityEngine;using System.Collections; public cla ...