//	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. java file类的常用方法和属性

    1 常用方法       a.createNewFile方法 public boolean createNewFile() throws IOException 该方法的作用是创建指定的文件.该方法只 ...

  2. 一看就懂得移动端rem布局、rem如何换算

    这里使用了js控制根元素的font-size大小,然后进行rem换算,在js代码后面会说明以下问题. 1.如何进行rem运算? 2.如果纯js控制根元素用rem布局会出现的小问题,如何解决? 3.如有 ...

  3. Android之数据库的创建

    一.SQLite介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP ...

  4. 网站appache的ab命令压力测试性能

    ①:相关不错的博文链接:http://johnnyhg.iteye.com/blog/523818 ②:首先配置好对应的环境上去,有对应的命令 ③:压力测试的指令如下: 1. 最基本的关心两个选项 - ...

  5. Eclipse安装部署(配图解)

    Eclipse安装部署 前提:已经成功搭建配置JDK 下载 eclipse, 下载地址: http://www.eclipse.org/downloads/ 解压缩安装包(注意安装路径中不可以有空格) ...

  6. SpringMVC框架搭建 基于注解

    本文将以一个很简单的案例实现 Springmvc框架的基于注解搭建,一下全为个人总结 ,如有错请大家指教!!!!!!!!! 第一步:创建一个动态web工程(在创建时 记得选上自动生成 web.xml ...

  7. FormatMessage与GetLastError配合使用,排查windows api调用过程中的错误

    前一段时间在学习windows api调用过程中,遇到过一些调用错误或者程序没能显示预期的结果,或者直接出现vc运行时错误. 这对新手来说是司空见惯的事,因为不太熟悉难免会出错,出错的信息如果能显示很 ...

  8. SpringMVC 400 Bad Request 问题

    摘要 SpringMVC 400 Bad Request 在提交表单时,发生400错误,并未进入save方法. @RequestMapping(value="/!save",met ...

  9. Arch Linux 简易打包指南

    本文时代久远,请参阅更可靠的:Arch User Repository (简体中文) - 分享和维护软件包 这两天给 Kreogist µ 打 Arch Linux 包,照着 wiki 跟着搞,同时在 ...

  10. 海量数据相似度计算之simhash短文本查找

    在前一篇文章 <海量数据相似度计算之simhash和海明距离> 介绍了simhash的原理,大家应该感觉到了算法的魅力.但是随着业务的增长 simhash的数据也会暴增,如果一天100w, ...