Computer Virus on Planet Pandora

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others) Total Submission(s): 1846    Accepted Submission(s): 510

Problem Description
    Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On  planet
Pandora, hackers make computer virus, so they also have anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern
string which consists of only capital letters. If a virus’s pattern string is a substring of a program, or the pattern string is a substring of the reverse of that program, they
can say the program is infected by that virus. Give you a program and a list of virus pattern strings, please write a program to figure out how many viruses the program
is infected by.
 
Input
There are multiple test cases. The first line in the input is an integer T ( T<= 10) indicating the number of test cases.
For each test case:
The first line is a integer n( 0 < n <= 250) indicating the number of virus pattern strings.
Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It’s guaranteed that those n pattern strings are all different so there are
n different viruses. The length of pattern string is no more than 1,000 and a pattern string at least consists of one letter.
The last line of a test case is the program. The program may be described in a compressed format. A compressed program consists of capital letters and  “compressors”.
A “compressor” is in the following format:
[qx]
q is a number( 0 < q <= 5,000,000)and x is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means  ‘KKKKKK’
in the original program. So, if a compressed program is like:
AB[2D]E[7K]G
It actually is ABDDEKKKKKKKG after decompressed to original format.
The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.
 
Output
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
 
Sample Input
3
2
AB
DCB
DACB
3
ABC
CDE
GHI
ABCCDEFIHG
4
ABB
ACDEE
BBB
FEEE
A[2B]CD[4E]F
 
Sample Output
0
3
2

 
Hint

In the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected
by virus ‘GHI’.

模板题:

  #pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std; typedef struct abcd
{
abcd *next[];
int end;
abcd *fail;
}abcd;
int ans;
abcd *inti()
{
abcd *t;
t=(abcd *)malloc(sizeof(abcd));
for(int i=;i<;i++)
t->next[i]=NULL;
t->end=;
t->fail=NULL;
return t;
}
void insert(abcd *t,char z[])
{
if(*z=='\0')
{
t->end++;
return;
}
if(t->next[*z-'A']==NULL)
t->next[*z-'A']=inti();
insert(t->next[*z-'A'],z+);
}
void ac(abcd *t)
{
queue<abcd*>a;
while(!a.empty())a.pop();
for(int i=;i<;i++)
{
if(t->next[i]!=NULL)
{
t->next[i]->fail=t;
a.push(t->next[i]);
}
else t->next[i]=t;
}
abcd *r,*f;
while(!a.empty())
{
r=a.front();
a.pop();
for(int i=;i<;i++)
{
if(r->next[i])
{
a.push(r->next[i]);
f=r->fail;
while(!f->next[i])f=f->fail;
r->next[i]->fail=f->next[i];
}
}
}
}
void query(abcd *t,char x[])
{
abcd *f,*p=t;
while(*x)
{
while(!p->next[*x-'A'])p=p->fail;
p=p->next[*x-'A'];
f=p;
while(f!=t&&f->end!=-)
{
ans+=f->end;
f->end=-;
f=f->fail;
}
x++;
}
x--;
p=t;
while(*x)
{
while(!p->next[*x-'A'])p=p->fail;
p=p->next[*x-'A'];
f=p;
while(f!=t&&f->end!=-)
{
ans+=f->end;
f->end=-;
f=f->fail;
}
x--;
}
}
void del(abcd *t)
{
for(int i=;i<;i++)
{
if(!t->next[i])
del(t->next[i]);
}
free(t);
}
int main()
{
int tr,n,i;
//cout<<"YES"<<endl;
cin>>tr;
while(tr--)
{
abcd *t;
t=inti();
char z[];
cin>>n;
for(i=;i<n;i++)
{
cin>>z;
insert(t,z);
}
ac(t);
char x[];
int tt=,r;
char xx;
x[]='\0';
xx=getchar();
xx=getchar();
while(xx!='\n')
{
if(xx!='[')
x[tt++]=xx;
else
{
cin>>r;
xx=getchar();
for(int i=;i<r;i++)
x[tt++]=xx;
xx=getchar();
}
xx=getchar();
}
x[tt]='\0';
ans=;
query(t,x+);
cout<<ans<<endl;
del(t);
}
}

hdu3695 ac自动机入门的更多相关文章

  1. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  2. AC自动机入门

    Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...

  3. HDU 2222 Keywords Search(AC自动机入门)

    题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. hdu2222之AC自动机入门

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

  6. hdu2222 ac自动机入门

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

  7. hdu 1277 AC自动机入门(指针版和数组版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...

  8. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  9. hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

    /** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...

随机推荐

  1. C++中const关键字用法

    为什么使用const?采用符号常量写出的代码更容易维护:指针常常是边读边移动,而不是边写边移动:许多函数参数是只读不写的.const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替 ...

  2. JQuery的动态加载class无法实现点击时间的解决方案

    //对于 加载过来class 的del_a 实现点击事情 $(document).on('click',".del_a",function(){ $(".mark_id& ...

  3. 如何使用EF优雅的配置一对一的关系

    在这两天的时间已经有两位同事问到EF(Code First)如何配置一对一的关系,这个说难也不难,说简单吧,一旦设计跑偏那么在Coding的过程中将会很痛苦. 先举个很简单的例子,两个类User和Pr ...

  4. JavaScript二维码生成——qrcode.js

    在开发中,有时候,我们需要根据不同的内容来动态生成二维码,则可以使用qrcode.js这个小插件来实现. 1.qrcode.js文件内容: (1)未压缩(qrcode.js): /** * @file ...

  5. 我的hibernate学习记录(一)

    之前已经过滤一下hibernate的简单的用法,但是近期有点时间,所以重新看下视频,敲下代码,翻下笔记,写博客与大家分享一下. hibernate简介 Hibernate是一个开放源代码的对象关系映射 ...

  6. CentOS7中将home迁移到/下的命令 CentOS7中将home迁移到/下的命令

    # mkdir -p /backup # cp -r /home/* /backup # umount /home #  df -hl # fdisk -l # lvremove /dev/cento ...

  7. Sqli-Labs学习总结一

    题目1-20 github地址 前言 以前对于SQL注入,就是先判断下能不能注入,可以的话先试着联合查询,不行的话再上SQLMap,去年寒假拿了一本<SQL注入攻击与防御>,拿回家,看了几 ...

  8. 201521123006 《java程序设计》 第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  9. Java课程设计--GUI密码生成器201521123033

    1.团队课程设计题目 基于GUI的密码生成器 团队博客链接 2.个人负责模块 (1)界面设计 (2)部分错误输入的提示 (3)一键复制密码功能的实现 3.个人代码的提交记录截图 4.个人代码展示以及代 ...

  10. 201521123018 《Java程序设计》第12周学习总结

    1. 本章学习总结 你对于本章知识的学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.da ...