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. webSocket浏览器握手不成功(解决)

    websocket与服务端握手会报握手不成功的错误解决方法: 首先是服务端首次收到请求要回报给客户端的报文要做处理多的不说,方法敬上: /// <summary> /// 打包请求连接数据 ...

  2. SQL Server锁类型

    SQL Server锁类型(SQL)收藏 1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁. 2. NOLOCK:不添加共享锁和排它锁,当这个选项生 ...

  3. 新CCIE笔记-IP网络基础

    南京捷式泰CCIE重修笔记:更完善更系统的全新笔记 新增内容: 总结.关联知识点.行业小建议 各种认证证书: RHCE VCP OCP MCSEPMP ITIL CCA CCIE CCNP CCNA ...

  4. 全面解析for循环

    牛刀小试: for(var i = 0 ; i < 100; i++) {console.log(i);} var i = 0;//第一个代码段 i < 100; //第二个代码段 i++ ...

  5. KVM网页管理工具WebVirtMgr部署

    KVM-WebVirtMgr 0ther https://github.com/retspen/webvirtmgr/wiki System Optimization(Only CentOS6.X) ...

  6. 【Alpha阶段】第七次scrum meeting

    一.会议照片 二.会议内容 姓名 学号 负责模块 昨日任务 今日任务 杨爱清 099 界面设计和交互功能 [完成]设计界面 交互功能连接并优化 杨立鑫 100 数据库搭建和其他 [完成]将数据库与其他 ...

  7. 201521123002《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.提交函数实 ...

  8. 201521123068《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 查看脑图->多态与接口 1.2 可选:使用常规方法总结其他上课内容. 2. 书面作业 1.代码阅读:Child压缩包内源 ...

  9. 201521123048 《Java程序设计》第2周学习总结

    本周学习总结 (1)String类数据类型 (2)枚举类型应用 (3)Java数组创建及使用 (4)进一步运用和了解码云 书面作业 Q1.使用Eclipse关联jdk源代码,并查看String对象的源 ...

  10. 201521123115 《Java程序设计》第1周学习总结

    1. 本周学习总结 之前一直没有用心学,这周上课虽然认真但还是太多不懂加上还要补考没有时间来消化所以很多都是囫囵吞枣,希望能够赶快补上把. 知道了java的前世今生,JVM/JRE/JDK,JVM就是 ...