/**
题目: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的待匹配串,问待匹配串中有哪几个模式串,的更多相关文章

  1. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  2. HDU2896 病毒侵袭 —— AC自动机

    题目链接:https://vjudge.net/problem/HDU-2896 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  3. hdu2896 病毒侵袭 ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...

  4. hdu2896病毒侵袭(ac自动机)

    链接 ac自动机的模板题 说2个注意的地方 一是题目说明包含所有ASCII字符,可以开到0-127 包含空格 题目会输入多个源串,在加完当前的val值时,不应清0,可以开个标记数组. #include ...

  5. hdu 2896 病毒侵袭 AC自动机 基础题

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. HDU2896 病毒侵袭 AC自动机模板

    各种MLE,这模板感觉有问题,next数组开128也会MLE,实际上可见字符为编号32~126,只用开100就行. #include <iostream> #include <cst ...

  7. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  8. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  9. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. POJ--Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 8370   ...

  2. 使用winsound模块播放声音

    import winsound import math s = """3345 1233345 234 431 434 -612 12334567 +1+1345 234 ...

  3. 将form表单转化为json数据

    参考地址:https://github.com/hongymagic/jQuery.serializeObject

  4. windows下使用mongodb

    ********************************************************************* ERROR: dbpath (\data\db\) does ...

  5. php 7.1安装教程

    一.下载地址 http://php.net/downloads.php#v7.1.9 IIS如果你使用的是PHP的FastCGI IIS,你应该使用非线程安全(NTS)版本的PHP. Apache请使 ...

  6. Latex--TikZ和PGF--高级文本绘图,思维绘图,想到--得到!

    Latex--TikZ和PGF--高级文本绘图,思维绘图,想到--得到! TikZ和PGF是一种用在TeX上的CLI绘图工具.CLI和GUI是两种常见的绘图方式,前者是所想即所得(WYTIWYG)的, ...

  7. 用rfkill命令管理蓝牙和wifi

    rfkill是一个内核级别的管理工具,可以打开和关闭设备的蓝牙和wifi. #列出所有可用设备rfkill list 输出如下:0: phy0: Wireless LAN    Soft blocke ...

  8. angular学习笔记(七)-迭代1

    本篇介绍angular中元素的迭代: <!DOCTYPE html> <html ng-app> <head> <title>4.1.迭代</ti ...

  9. 【快速查阅】SQLPLUS连接ORACLE

    使用SQLPLUS连接ORACLE常用的有两种方式. 一.简易方式 sqlplus 用户名/密码@IP或主机名:端口/数据库服务名称 二.预先配置TNSNAMES的方式 在“%ORACLE_HOME% ...

  10. vue中sass的配置安装流程

    1.安装node-sass,因为scss是基于此库的 cnpm install --save-dev node-sass 2.安装sass-loader cnpm install --save-dev ...