题意:输出出现模式串的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. spring中使用@Value设置全局变量默认值

    前几天在开发过程中遇到一个使用 spring 的 @Value 给类的全局变量设置默认值不成功的问题,最后通过查资料也是轻松解决,但是发现使用@Value也是有多种多样的方式,今天总算是将开发任务结束 ...

  2. HDU 1213(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. 【BZOJ 4170】 4170: 极光 (CDQ分治)

    4170: 极光 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 121  Solved: 64 Description "若是万一琪露诺(俗 ...

  4. 如何在SpringBoot当中上传多个图片或者上传单个图片 工具类

    如何在SpringBoot当中上传多个图片[上传多个图片 ] 附赠工具类 1.SpringBoot 上传图片工具类 public class SpringUploadUtil { /*** * 上传图 ...

  5. bzoj1123 Blockade

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  6. [转]runOnUiThread 、 Handler 对比(一)

      this.runOnUiThread(new Runnable() { @Override public void run() { try { Thread.sleep(1000 * 5); }  ...

  7. MSChart使用小结

        在用到图表展示某项.多项信息的统计情况,很正常联想到MSChart控件.        以VS2008开发为例,在工具箱也中右击,选择”choose items“,打开对话框,选择COM组件T ...

  8. InvalidateRect()与Invalidate()的用法(转)

    BOOL InvalidateRect(   HWND hWnd,           // 窗口句柄   CONST RECT* lpRect,   // 矩形区域   BOOL bErase    ...

  9. Caliburn.Micro对目录结构的要求

    Caliburn.Micro对MVVM目录结构的要求判定规则是如下正则表达式: (?<nsbefore>([A-Za-z_]\w*\.)*)(?<subns>ViewModel ...

  10. Linux下动态共享库加载及使用详解

    转载;http://blog.chinaunix.net/uid-29025972-id-3855500.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loa ...