题意:输出出现模式串的id,还是用end记录id就可以了。

本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车)。”  -----也就说AC自动机的Trie树需要128个单词分支。

#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int maxw = 210 *500 + 10;
const int sigma_size = 128;
const int maxl = 10000 + 10; struct Trie{
int next[maxw][sigma_size],fail[maxw],end[maxw];
int root,L;
int newnode(){
for(int i=0;i<sigma_size;i++)
next[L][i]=-1;
end[L++]=-1;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void insert(const char *s,int id){
int now=root,len=strlen(s);
for(int i=0;i<len;i++){
if(next[now][s[i]]==-1)
next[now][s[i]]=newnode();
now=next[now][s[i]];
}
end[now]=id;
}
void build(){
queue<int>Q;
fail[root]=root;
for(int i=0;i<sigma_size;i++)
if(next[root][i]==-1)
next[root][i]=root;
else{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now=Q.front();
Q.pop();
for(int i=0;i<sigma_size;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
bool used[500 + 10];
bool query(const char *buf,int n,int id){
int now=root,len=strlen(buf);
memset(used,false,sizeof(used));
bool flag =false;
for(int i=0;i<len;i++){
now=next[now][buf[i]];
int tmp=now;
while(tmp!=root){
if(end[tmp]!=-1){
used[end[tmp]]=true;
flag=true;
}
tmp=fail[tmp];
}
}
if(!flag) return false;
printf("web %d:",id);
for(int i=1;i<=n;i++)
if(used[i]) printf(" %d",i);
printf("\n");
return true;
}
}; char buf[maxl];
Trie ac; int main()
{
int n,m;
while(~scanf("%d",&n)){
ac.init();
for(int i=1;i<=n;i++){
scanf("%s",buf);
ac.insert(buf,i);
}
ac.build();
int ans=0;
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%s",buf);
if(ac.query(buf,n,i)) ans++;
}
printf("total: %d\n",ans);
}
return 0;
}

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

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

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

  2. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  4. HDU 2896 AC自动机 裸题

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

  5. hdu 3065 AC自动机模版题

    题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...

  6. HDU 2222 AC自动机(模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. hdu 2896 AC自动机

    // hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...

  8. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

随机推荐

  1. 深入理解javascript作用域系列第五篇

    前面的话 对于执行环境(execution context)和作用域(scope)并不容易区分,甚至很多人认为它们就是一回事,只是高程和犀牛书关于作用域的两种不同翻译而已.但实际上,它们并不相同,却相 ...

  2. 【UOJ 34】 #34. 多项式乘法 (FFT)

    [分析] 这个只是用来放模板..[其实我还没完全懂的.. 迭代 代替 递归: #include<cstdio> #include<cstdlib> #include<cs ...

  3. [APIO2007]风铃 --- 贪心

    [APIO2007]风铃 题目描述 你准备给弟弟 Ike 买一件礼物,但是,Ike 挑选礼物的方式很特别:他只喜欢那些能被他排成有序形状的东西. 你准备给 Ike 买一个风铃.风铃是一种多层的装饰品, ...

  4. [BZOJ4237]稻草人(CDQ分治)

    先按y排序,二分,两边递归下去,然后处理下半部分对上半部分的贡献,即左下点在下半部分,右上点在上半部分的合法矩形个数. 两个部分均按x排序,枚举右上点p,则左下点需要满足: 1.横坐标大于上半部分纵坐 ...

  5. 快速傅里叶变换(FFT)相关内容汇总

    (原稿:https://paste.ubuntu.com/p/yJNsn3xPt8/) 快速傅里叶变换,是求两个多项式卷积的算法,其时间复杂度为$O(n\log n)$,优于普通卷积求法,且根据有关证 ...

  6. Codeforces Round #299 (Div. 1) A. Tavas and Karafs 水题

    Tavas and Karafs Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/536/prob ...

  7. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  8. JAVA容器-模拟LinkedList实现(双链表)

    概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...

  9. KVM虚拟机安装使用教程(Ubantu)

    背景: 公司在某电信机房有50台ubantu的实体机,机器配置的ip是192.168.100.x的ip,内存和cpu都是高端配置.假如哪些端口需要对外映射,就通过机房的防火墙完成端口映射. 100.1 ...

  10. POJ 3237 Tree (树链剖分)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 2825   Accepted: 769 Description ...