//	hdu 2896 AC自动机
//
// 题目大意:
//
// 给你n个短串,然后给你q串长字符串,要求每个长字符串中
// 是否出现短串,出现的短串各是什么
//
// 解题思路:
//
// AC自动机,插入单词,构建自动机,然后查询就可以了。
//
// 感悟:
//
// 哎,自己AC自动机果然刚学,还只会个模板,自动机构没构建
// 都不知道。。。继续加油吧~~~FIGHTING! #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAX_NODE = 200000; const int SIGMA_SIZE = 131; struct Aho_Corasick{ int ch[MAX_NODE][SIGMA_SIZE];
int val[MAX_NODE];
int cnt[508];
int sz;
int last[MAX_NODE];
int f[MAX_NODE]; void clear(){
memset(cnt,0,sizeof(cnt));
} void init(){
sz = 1;
memset(ch[0],0,sizeof(ch[0]));
val[0] = 0;
} int idx(char c){
return c - 'a';
} void insert(char *s,int v){
int u = 0;
int n = strlen(s);
for (int i=0;i<n;i++){
//int c = idx(s[i]);
int c = s[i];
if (!ch[u][c]){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} void getfail(){
queue<int> que;
for (int c = 0; c < SIGMA_SIZE ; c++){
int u = ch[0][c];
if (u){
que.push(u);
last[u] = 0;
f[u] = 0;
}
}
while(!que.empty()){
int r = que.front();
que.pop();
for (int c = 0; c < SIGMA_SIZE ; c++){
int u = ch[r][c];
if (!u)
continue;
que.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]]; }
}
} void get_cnt(int u){
if (u){
cnt[val[u]]++;
get_cnt(last[u]);
}
} void query(char *s){
int u = 0;
int n = strlen(s);
for (int i=0;i<n;i++){
//char c = idx(s[i]);
char c = s[i];
while( u && !ch[u][c])
u = f[u]; u = ch[u][c]; if (val[u]){
get_cnt(u);
}
else if (last[u]){
get_cnt(last[u]);
}
}
} }ac; char str[10009]; int n; void print(int x,int &num){
int flag = 0;
for (int i=1;i<=n;i++){
if (ac.cnt[i]){
flag = 1;
break;
}
}
if (!flag)
return; printf("web %d:",x); for (int i=1;i<=n;i++){
if (ac.cnt[i]){
printf(" %d",i);
}
}
puts("");
num++; } void input(){
ac.init();
for (int i=1;i<=n;i++){
scanf("%s",str);
ac.insert(str,i);
}
int q;
int num = 0;
ac.getfail();
scanf("%d",&q);
int x = 1;
for (int i=1;i<=q;i++){
scanf("%s",str);
ac.clear();
ac.query(str);
print(i,num);
}
printf("total: %d\n",num);
} int main(){
//freopen("1.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
input();
}
}

hdu 2896 AC自动机的更多相关文章

  1. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  2. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...

  3. HDU 2896 AC自动机 裸题

    中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...

  4. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  5. hdu 5880 AC自动机

    Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. hdu 3065 AC自动机(各子串出现的次数)

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

  9. HDU 5384 AC自动机

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...

随机推荐

  1. Xilium.CefGlue CEF Chrome 自动上传文件不弹出对话框 CefDialogHandler

    关键代码如下,如何使用Handler,还不清楚的请继续搜索 internal sealed class WyzCefDialogHandler : CefDialogHandler    {      ...

  2. openstack快速封装镜像

    本来是该写对接ceph的,但是临时有事了,必须先弄镜像,因此提前写这个了,我个人不是按官网来封装镜像的,是按自己的习惯,所以觉得信不过的可以用官网的方式,有兴趣的可以看看都知道,openstack创建 ...

  3. java 零碎知识点

    1. 字符串有整型的相互转换 1 2 String a = String.valueOf(2);   //integer to numeric string  int i = Integer.pars ...

  4. SQL基本语句以及示例

    基本语句: /*dorp colunm*/ 语法:ALTER TABLE 表名   DROP COLUMN 要删除的字段 验证财务转换的正确性,查询以下两个表是否有数据 /*表连接inner jion ...

  5. design philosophy

  6. iOS开发】canOpenURLl 和修改http请求

    控制台输出 如图是在我启动一个 Xcode + iOS 的 App 之后,控制台的输出. 这在 Xcode 时,是不会有的情况,原因是[为了强制增强数据访问安全, iOS9 默认会把所有从NSURLC ...

  7. OC基础语法之方法

    看惯了c#的代码再去看Object-C的代码,总感觉有点懵逼,记录下OC的方法用法:   在OC中一个类中的方法有两种类型:实例方法(类似于非静态函数),类方法(类似非静态函数). 实例方法前用(-) ...

  8. get_object_vars($var) vs array($var)

    get_object_vars(\(var) vs array(\)var) test case class Test { public function actionGetObjectVarsVsA ...

  9. TCP/IP详解系列 --- 概念总结01

    UDP协议  .vs.  TCP协议:  原理上:(TCP报文段. vs . UDP用户数据报) TCP协议的特性: TCP是面向连接的运输层协议,应用程序在使用TCP协议之前,必须先建立TCP连接. ...

  10. .net加密解密

    .net加密概述 .net加密编程模型 .net加密解密类