HDU2896+AC自动机
ac自动机 模板题
/* */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int maxm = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-; struct Tree{
int id;
Tree *fail;
Tree *next[ maxn ];
};
Tree root;
Tree *q[ ];
char str[ maxm ];
int tot;
int cnt[ ];
int Index_cnt ;
bool vis[ ]; void init(){
root.id = ;
root.fail = NULL;
for( int i=;i<maxn;i++ )
root.next[ i ] = NULL;
} void build( char str[],int ID ){
int len = strlen( str );
Tree *p = &root;
Tree *tmp = NULL;
for( int i=;i<len;i++ ){
int id = str[i]-;
if( p->next[ id ]==NULL ){
tmp = (Tree *)malloc(sizeof( root ));
tmp->id = ;
for( int j=;j<maxn;j++ )
tmp->next[ j ] = NULL;
p->next[ id ] = tmp;
p = p->next[ id ];
}
else
p = p->next[ id ];
}
p->id = ID;
return ;
} void build_AC(){
int head = ,tail = ;
Tree *p = &root;
Tree *tmp = NULL;
q[ tail++ ] = p;
while( head!=tail ){
p = q[ head++ ];
for( int i=;i<maxn;i++ ){
if( p->next[i]!=NULL ){
if( p==(&root) ){
p->next[i]->fail = &root;
}
else{
tmp = p->fail;
while( tmp!=NULL ){
if( tmp->next[i]!=NULL ){
p->next[i]->fail = tmp->next[i];
break;
}
tmp = tmp->fail;
}
if( tmp==NULL )
p->next[i]->fail = &root;
}
q[ tail++ ] = p->next[i];
}
}
}
} void query( char str[] ){
int len = strlen( str );
Tree *p = &root;
Tree *tmp = NULL;
for( int i=;i<len;i++ ){
int id = str[i]-;
while( p->next[id]==NULL&&p!=(&root) ){
p = p->fail;
}
p = p->next[ id ];
if( p==NULL )
p = &root;
tmp = p;
while( tmp!=(&root) ){
if( tmp->id!=&&vis[tmp->id]==false ){
vis[tmp->id] = true;
cnt[ Index_cnt++ ] = tmp->id;
}
tmp = tmp->fail;
}
}
return ;
} int main(){
int n,m;
while( scanf("%d",&n)!=EOF ){
init();
char s[ ];
for( int i=;i<=n;i++ ){
scanf("%s",s);
build( s,i );
}
build_AC();
scanf("%d",&m);
tot = ;
for( int i=;i<=m;i++ ){
scanf("%s",str);
Index_cnt = ;
memset( vis,false,sizeof( vis ) );
query( str );
if( Index_cnt!= ) {
tot++;sort( cnt,cnt+Index_cnt );
printf("web %d:",i);
for( int j=;j<Index_cnt;j++ )
printf(" %d",cnt[j]);
printf("\n");
}
}
printf("total: %d\n",tot);
}
return ;
}
HDU2896+AC自动机的更多相关文章
- HDU2896(AC自动机入门题)
病毒侵袭 Time Limit:1000MS Memory Limit:32768KB Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...
- 【HDU2896】病毒侵袭 AC自动机
[HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- HDU2896:病毒侵袭(AC自动机)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu2896病毒侵袭(ac自动机)
链接 ac自动机的模板题 说2个注意的地方 一是题目说明包含所有ASCII字符,可以开到0-127 包含空格 题目会输入多个源串,在加完当前的val值时,不应清0,可以开个标记数组. #include ...
- hdu2896 病毒肆虐【AC自动机】
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 病毒侵袭---hdu2896(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 输入的字符是所有可见的ASCII码(共有127个)所以要注意一下: 把结果存到一个数组中,然后输 ...
- HDU2896【AC自动机-模板】
思路: 因为不同病毒特征码不会相同. AC自动机,然后对于每一个输出即可. 注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE. //#include <bits/st ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
随机推荐
- 注意java的对象引用
要注意,当前拿到的“对象引用”, 是不是 指向 最新的实例, 没有的话, 要重新 生成实例去指向. 代码例子: AnsweringRuleInfo bhRule = accountGenerator. ...
- 第六十四篇、OC_计步器
计步器的实现方式主要有那么两种 1.通过直接调用系统的健康数据,基于HealthKit框架的,但是貌似是一小时更新一次数据.如果要实时获取步数,这种方式并不是最佳. 2.基于CoreMotion框架, ...
- JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记1
技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] 第 3 章 用 JavaScri ...
- StringBuilder和StringBuffer
StringBuilder java.lang 类 StringBuilder java.lang.Object java.lang.StringBuilder 所有已实现的接口: Serializa ...
- UVALive 3027(并查集)
题意:某公司的各企业群要建立联系,I i j 表示企业i与企业j建立联系,并且以企业j为中心(并查集中的父亲)(企业j为暂时的中心企业),E i 表示查询企业 i 距离此时的中心企业的距离.各企业间的 ...
- 算法 replace,replace_copy,back_inserter
replace (list.begin(), list.end(), , ); // replace any elements with value of 0 by 42 replace算法对输入序列 ...
- HR不会告诉你的秘密
原文转载自http://blog.csdn.net/happy08god/article/details/5534326 下面,只是摘出来一些基本的观点. 1. 入职时的工资高低不重要,只要你努力工作 ...
- CSDN 自动评论
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/csdn-auto-reviews/ 说明 当打开http://download.csdn.net/my/do ...
- 使用JavaScript实现简单的输入校验
HTML页面代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...
- jQuery获取同级元素
next()相邻下一个同级元素 prev()相邻上一个同级元素 siblings()所有同级元素 $("#id").next(); $("#id").prev( ...