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. python---统计列表中数字出现的次数

    import collections a = [1,2,3,1,2,3,4,1,2,5,4,6,7,7,8,9,6,2,23,4,2,1,5,6,7,8,2] b = collections.Coun ...

  2. 自动化测试:behave

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. js script放在head和body里面的区别

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp66       java script放在head和body的区别   ...

  4. Window下SVN服务器搭建以及客户端使用

    一.下载 上一篇博客是关于Jenkins的内容,在Jenkins自动化编译时可能会自动获取版本更新进行build,那就需要用到版本更新的工具.这里使用VisualSVN Server来作为搭建svn的 ...

  5. 团队作业1 团队展示&选题

    团队展示&选题 Coding项目地址:https://git.coding.net/wjunren/running.git 一.团队展示 1.队名:Runing Guys 2.队员: 组长:骆 ...

  6. 团队作业4——第一次项目冲刺(Alpha版本)5th day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 计时模式已经大致完成了 接下来是记录成绩的部分 四.困难与问题 1.新语言的学习与适应较慢,整体的开发进展达不到预期效果, 2 ...

  7. 学号:201521123116 《java程序设计》第七周学习总结

    1. 本周学习总结 2. 书面作业 Q1 ArrayList代码分析 1.1 解释ArrayList的contains源代码ArrayList的contains源代码 1.2 解释E remove(i ...

  8. 201521123102 《Java程序设计》第2周学习总结

    #1. 本周学习总结(1)学习使用码云存储代码(2)掌握了常见数据类型的使用.转换(3)回顾了前面学过的基本语法(4)复习一二三章内容 #2. 书面作业**Q1.使用Eclipse关联jdk源代码,并 ...

  9. locale命令设置语言环境

    locale命令设置语言环境 在Linux中通过locale来设置程序运行的不同语言环境,locale由 ANSI C提供支持.locale的命名规则为_.,如zh_CN.GBK,zh代表中文, CN ...

  10. 201521123009 《Java程序设计》第9周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 Q1:常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何 ...