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自动机的更多相关文章

  1. HDU2896(AC自动机入门题)

    病毒侵袭 Time Limit:1000MS     Memory Limit:32768KB   Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...

  2. 【HDU2896】病毒侵袭 AC自动机

    [HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...

  3. hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,

    /** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...

  4. HDU2896:病毒侵袭(AC自动机)

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

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

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

  6. hdu2896 病毒肆虐【AC自动机】

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

  7. 病毒侵袭---hdu2896(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 输入的字符是所有可见的ASCII码(共有127个)所以要注意一下: 把结果存到一个数组中,然后输 ...

  8. HDU2896【AC自动机-模板】

    思路: 因为不同病毒特征码不会相同. AC自动机,然后对于每一个输出即可. 注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE. //#include <bits/st ...

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

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

随机推荐

  1. jQuery 笔记

    1. 选择器  http://www.runoob.com/jquery/jquery-selectors.html 2. toggle()  用来切换 hide() 和 show() 方法   ht ...

  2. ios开发入门篇(一):创建工程

    突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...

  3. POJ 2528 Mayor’s posters

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37982   Accepted: 11030 ...

  4. MySQL学习笔记之数据存储类型

    说明:本文是作者对MySQL数据库数据存储类型的小小总结. Numeric Type (数字类型) 1.TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT主要根据存储字节长度不 ...

  5. 修改SSH端口为21

    在交流群里面有一位兄弟问到能否将ssh端口号修改为21端口,后来经过测试可以设置,具体步骤如下: 一.修改ssh配置文件的默认端口 #vim /etc/ssh/sshd_config 找到#port ...

  6. Mysql创建函数时报错

    先去查询  show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_f ...

  7. Android 控件收集

    SwipeMenuExpandableListView   https://github.com/tycallen/SwipeMenu-Expandable-ListView

  8. 从零学起PHP

    数据库连接conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root", ...

  9. WordPress非插件添加文章浏览次数统计功能

    一: 转载:http://www.jiangyangangblog.com/26.html 首先在寻找到functions.php.php文件夹,在最后面  ?> 的前面加入下面的代码 func ...

  10. java之StringBuffer

    StringBuffer就是字符串缓冲区,用于存储数据的容器. 特点:长度可变,可存储不同类型的数据,最终转化成字符串使用,可以对字符串修改 功能: 添加:append(value), insert( ...