题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896

输入的字符是所有可见的ASCII码(共有127个)所以要注意一下;

把结果存到一个数组中,然后输出;

ac自动机详解:http://www.cnblogs.com/zhengguiping--9876/p/4854752.html

要用c++交,G++会MLE的;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; const int N = 1e4+;
int vis[N], a[N], cnt;
char s[N];
struct node
{
int leaf;
node *next[],*fail;
}; void AddTrie(char s[], node *root, int num)
{
node *p = root;
for(int i=; s[i]; i++)
{
int k = (int)s[i];
if(p->next[k]==NULL)
p->next[k] = new node();
p = p->next[k];
}
p->leaf = num;
}
void GetFail(node *root)
{
node *p, *q;
queue<node*>Q;
Q.push(root);
while(Q.size())
{
p = Q.front(); Q.pop();
for(int i=; i<; i++)
{
if(p->next[i]!=NULL)
{
q = p->fail;
while(q!=NULL)
{
if(q->next[i]!=NULL)
{
p->next[i]->fail = q->next[i];
break;
}
q = q->fail;
}
if(q==NULL)
p->next[i]->fail = root;
Q.push(p->next[i]);
}
}
}
} void Query(char s[], node *root)
{
node *p = root, *q;
for(int i=; s[i]; i++)
{
int k = (int)s[i]; while(p->next[k] == NULL && p!=root)///如果匹配不成功,那么就走失败路径,沿着fail指针走,一直到找到或者跟结点为止;
p = p->fail;
p = p->next[k];
if(p==NULL)
p = root;
q = p;
while(q!=root)
{
if(q->leaf && !vis[q->leaf])
{
a[cnt++] = q->leaf;
vis[q->leaf] = ;
}
q = q->fail;
}
}
}
void FreeTrie(node *root)
{
node *p = root;
for(int i=; i<; i++)
{
if(p->next[i]!=NULL)
FreeTrie(p->next[i]);
}
free(p);
}
int main()
{
int n, m;
while(scanf("%d", &n)!=EOF)
{
node *root = new node();
for(int i=; i<=n; i++)
{
scanf("%s", s);
AddTrie(s, root, i);
}
GetFail(root);
scanf("%d", &m);
int ans = ;
for(int i=; i<=m; i++)
{
scanf("%s", s);
memset(a, , sizeof(a));
memset(vis, , sizeof(vis));
cnt = ;
Query(s, root);
if(cnt)
{
ans++;
printf("web %d:", i);
sort(a, a+cnt);
for(int j=; j<cnt; j++)
printf(" %d", a[j]);
printf("\n");
}
}
printf("total: %d\n", ans);
FreeTrie(root);
}
return ;
}

病毒侵袭---hdu2896(AC自动机)的更多相关文章

  1. HDU2896:病毒侵袭(AC自动机)

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

  2. HDU2896 病毒侵袭 【AC自动机】

    HDU2896 病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一 ...

  3. HDU 2896 病毒侵袭(AC自动机)

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

  4. HDU 2896 病毒侵袭(AC自动机水)

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

  5. HDU 2896 病毒侵袭【AC自动机】

    <题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...

  6. hdoj 2896 病毒侵袭(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另 ...

  7. HDU 2896:病毒侵袭(AC自动机)

    http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:中文题意. 思路:AC自动机模板题.主要在于字符有128种,输出还要排序和去重! 注意是“total ...

  8. [HDU2896]病毒侵袭(AC自动机)

    传送门 题目中文描述,赞! 除了val记录id以外就是模板. 注意:每次数组都要清0.0 ——代码 #include <cstdio> #include <queue> #in ...

  9. HDU 2896 病毒侵袭 【AC自动机】

    HDU 2222 仅仅求出了和文本串匹配的模式串个数,本题要求求出匹配的模式串的编号. 不同的部分在代码中的注释部分. #include <cstdio> #include <cst ...

  10. HDU 2896 病毒侵袭 (AC自动机)

    这题模板题.............但是竟然要去重........调试了半天才发现.................... #include <cstdio> #include <i ...

随机推荐

  1. hdu1003 最大子串和

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. (转载) STL中map用法详解

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  3. 跟着百度学PHP[11]-PHP当中的异常处理

    首先要说一下常见的三种错误: 1.语法错误 2.运行错误 3.逻辑错误 00x1 错误报告及错误级别 PHP的错误分为三个等级 1.注意(notice)   没有变量a 2.警告(warning) 没 ...

  4. 网页打开新窗口——Window.open()详解

    转载自:http://blog.csdn.net/business122/article/details/8281142 Window.Open详解 一.window.open()支持环境:JavaS ...

  5. Odoo ParseError:"decoder jpeg not available" while parsing....

    The reason causing this problem is the plugin PIL install error to solve this problem,try this: 1. c ...

  6. input checkbox 选中问题

    对html控制不熟的人,估计被checkbox的选中问题发愁了,因为input的checkbox只有选中属性 checked='checked' 但是它有另外一个规则就是Request的时候 只有选中 ...

  7. thinkphp 原生sql使用分页类

    public function index(){ import("@.ORG.Page"); //导入分页类 $Dao = M(); $count = count($Dao-> ...

  8. java----序列化与反序列化中及java序列化本质就是存储一个对象,然后在其他地方在调用它

    Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是 ...

  9. 【BZOJ】1677: [Usaco2005 Jan]Sumsets 求和(dp/规律)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1677 完全背包很容易想到,将1,2,4...等作为物品容量即可. 然后这题还有一个递推式 f[i]= ...

  10. 将数据写入TXT文件中,file_put_contents与fwrite

    <?php header("content-type:text/html;charset=utf-8"); $file = './aa.txt'; ###判断是不是文件 if ...