病毒侵袭

模板题,不多说了。。

题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数。格式请看样例。

思路:典型AC自动机模板题,建一个Trie树,叶节点赋病毒的编号,然后依次匹配,用set存入编号即可,如果不带病毒不用输出来。这题很容易MLE,动态建树释放内存,所以采用静态建树,可以参考POJ Phone List。注意用C++提交。

const int N=129;//可见字符很多,注意不能仅用26
char str[10001],s[205];
int head,tail,cnt;
set<int>g[1005];
struct tree
{
int f;
tree *fail;
tree *next[N];
}*q[500010],*root,memory[500010];
void insert(char *s,int i)
{
tree *p=root;
while(*s!='\0')
{
int id=(int)(*s);
if(p->next[id]==NULL) p->next[id]=&memory[cnt++];
p=p->next[id];
s++;
}
p->f=i;
}
void build()
{
root->fail=NULL;
q[head++]=root;
while(head!=tail)
{
tree *temp=q[tail++];
tree *p=NULL;
for(int i=0;i<N;i++)
if(temp->next[i])
{
if(temp==root) temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i])
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL) temp->next[i]->fail=root;
}
q[head++]=temp->next[i];
}
}
}
void find(int i,char *s)
{
tree *p=root;
while(*s!='\0')
{
int id=(int)(*s);
while(p->next[id]==NULL&&p!=root) p=p->fail;
p=p->next[id];
p=p==NULL?root:p;
tree *temp=p;
while(temp!=root)
{
if(temp->f!=0) g[i].insert(temp->f);
temp=temp->fail;
}
s++;
}
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
cnt=head=tail=0;
int tot=0;
root=&memory[cnt++];
for(int i=0; i<1005; i++) g[i].clear();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
insert(s,i);
}
build();
scanf("%d",&m);
int t=1;
while(m--)
{
scanf("%s",str);
find(t,str);
t++;
if(g[t-1].size())
{
printf("web %d:",t-1);
for(set<int>::iterator it=g[t-1].begin(); it!=g[t-1].end(); it++)
printf(" %d",*it);
printf("\n");
tot++;
}
}
printf("total: %d\n",tot);
}
return 0;
} /*****************************************
***************** LYQ ***************
***************** YES ***************
*UserID: secrecy *
*RunOJ: *
*RunID: *
*Submit time: *
*Language: G++ *
*Result: Accepted *
*time: *
*Memory: *
*Length: *
*School: NYIST *
*Blog: http://blog.csdn.net/nyist_tc_lyq *
*QQ: *
*Tel: *
*****************************************/

HDu-2896 病毒侵袭,AC自动机模板题!的更多相关文章

  1. hdu 2896 病毒侵袭 AC自动机 基础题

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

  2. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  3. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

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

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

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  5. hdu 2896 病毒侵袭_ac自动机

    题意:略 思路:套用ac自动机模板 #include <iostream> #include<cstdio> #include<cstring> using nam ...

  6. hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,

    /** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...

  7. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. andorid 向上滑动控制标题栏显示

    要实现这样的功能,原理不难,监听滑动距离再设置标题栏的透明度 下面是监听一个带头view的list实现核心代码: mbar是标题栏 mListView.setOnScrollListener(new ...

  2. (转)UVM挑战及概述

    UVM的调度也具有其独特的挑战,尤其是在调试的领域.其中的一些挑战如下: 1. Phase的管理:objections and synchronization 2. 线程调试 3. Tracing i ...

  3. Python实现扫描作业配置自动化

    持续集成平台接入扫描作业是一项繁琐而又需要细致的工作,于是趁着闲暇时间,将代码扫描作业用Python代码实现了配置自动化. 每次配置作业的过程中,都会在checkcode1或者checkcode3上 ...

  4. 洛谷 P1009 阶乘之和

    题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一个正整数S,表示计算结 ...

  5. Servlet和JSP之标签文件学习

    在上一篇文章中介绍了自定义标签的用法,接下来介绍标签文件的用法啦. tag file指令 tag file简介 用tag file的方式,无需编写标签处理类和标签库描述文件,也可以自定义标签.tag ...

  6. 使用javap深入理解Java整型常量和整型变量的区别

    我下图代码第五行和第九行分别定义了一个整型变量和一个整型常量: static final int number1 = 512; static int number3 = 545; Java程序员都知道 ...

  7. Core Foundation 框架

    Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能.下面列举该框架支持进行管理的数据以及可提供的 ...

  8. int型除以int型

    int型除以int型得到的还是int型 就算你是这样的:float a = 5/3,虽然你定义的a是float型,但a得到的结果依旧是1.0000而不是1.66666 5/3先得到1,然后再转换成1. ...

  9. pytorch中的view

    https://ptorch.com/news/59.html view()相当于reshape(),其中参数若为-1表示当前的size根据其余size推断

  10. PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)

    PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...