//	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. Devexpress WidgetView 使用总结

    效果图: 1.添加DocumentManager控件 2.Convert to WidgetView 3.添加Document,设定ControlTypeName属性,第5步用到 4.添加StackG ...

  2. iOS 键盘类型

    版权声明:本文为博主原创文章.请尊重作者劳动成果,转载请注明出处. UIKeyboardTypeDefault: UIKeyboardTypeASCIICapable: UIKeyboardTypeN ...

  3. history对象的一些知识点

    history对象可以保存用户的上网的历史记录,即从窗口被打开的那一刻算起.这里有个比较纠结的问题,出于安全因素的考虑,开发人员无法得知用户浏览过的URL, 只能通过用户访问过的页面列表,实现后退和前 ...

  4. POI读取Excel内容格式化

    在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...

  5. html只给自己

    //另外一个 height:400px; weight:400px; border-top-left-radius: 10px; border-top-right-radius: 10px; bord ...

  6. C# 去除字符串首尾字符或字符串

          在做一个属性入库的功能,将Excel属性数据导入到图层要素当中,这里Excel和SDE数据库数据存在一个关联字段,通过关联字段值进行匹配属性入库.       在实际业务中,由于普查数据往 ...

  7. X64操作系统组件Jmail无法正常服务问题

    故障现象: 近日,在VMware虚拟化部署迁移中,之前物理服务器中部署网站ASP组件Jmail服务一切正常,迁移完成后发现Jmail无法正常工作,其余组件能正常工作. 环境:Windows Serve ...

  8. JavaScript数组删除重复元素(去重)的方法

    1. var arr=str.split(""); ;arr.length->i;i++){ ;j<arr.length;j++){ if(arr[j]==arr[i] ...

  9. Nginx配置upstream实现负载均衡

    如果Nginx没有仅仅只能代理一台服务器的话,那它也不可能像今天这么火,Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用.具体配置过程如下: 1. 在http节点下,添加ups ...

  10. c# htmtToPDF

    http://www.cnblogs.com/shanyou/archive/2012/09/07/2676026.html