hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/128000 K (Java/Others)
Total Submission(s): 2975 Accepted Submission(s):
833
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 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.
meaning that the program is infected by K viruses.
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’.
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<iostream> using namespace std; const int maxn=;
struct Trie{ Trie *child[maxn];
Trie * fail ;
int tail; //函数不占用内存空间
void init(){
for(int i=;i<maxn ;i++ )
child[i]=NULL;
fail=NULL;
tail=;
} }; char var[] ,stt[];
char stmp[]={'\0'}; //构建一个Trie树
void BuildTrie(char st[] , Trie * root){ Trie * cur ;
for( int i=; st[i] ; i++ ){
int ps=st[i]-'A';
if(root->child[ps]==NULL){
cur = new Trie;
cur->init(); //初始化
root->child[ps]=cur;
}
root = root->child[ps];
} root->tail++;
} //构造失败指正
void BuildFail(Trie * root){
queue<Trie *> sav;
Trie * tmp,*cur ;
sav.push(root);
while(!sav.empty()){
tmp = sav.front();
sav.pop();
for( int i= ; i<maxn ; i++ ){
if(tmp->child[i]!=NULL){
if(tmp==root) {
tmp->child[i]->fail=root;
}
else{
cur =tmp;
while(cur->fail){
if(cur->fail->child[i]!=NULL){
tmp->child[i]->fail = cur->fail->child[i];
break;
}
cur =cur->fail;
} if(cur->fail==NULL)
tmp->child[i]->fail = root ; }
sav.push(tmp->child[i]);
}
}
}
} //查询
int Query( char ss[] , Trie *root){ Trie * tmp=root ,*cur;
int res=,ps=;
for(int i=; ss[i] ;i++){
ps = ss[i]-'A';
while(tmp->child[ps]==NULL&&tmp!=root){
tmp=tmp->fail;
}
tmp = tmp->child[ps];
if(tmp==NULL) tmp=root;
cur=tmp;
while(cur!=root&&cur->tail>){
res+=cur->tail;
cur->tail=;
cur= cur->fail;
}
}
return res ;
} //对字符串进行必要的伸展
int change(char stt[] ,char stmp []){ int i,j;
for(i=,j=; stt[i] ;i++){
if(stt[i]!='['){
stmp[j++]=stt[i];
}else{
int k=,lvar=;
while(stt[++i]>=''&&stt[i]<=''){
lvar=lvar*+(stt[i]-'');
}
for(k=;k<lvar;k++){
stmp[j++]=stt[i];
}
i++;
}
}
stmp[j]='\0';
return j;
} //交换字符
void swap(char * a,char *b){
if(*a!=*b) *a^=*b^=*a^=*b;
}
//对字符串进行反序输入 void Reverse(char * ss,int len){ for(int i=;i<len/;i++){
swap(ss[i],ss[len-i-]);
}
} int main(){ int tes,nm,res;
scanf("%d",&tes);
while(tes--){
res=;
Trie * root = new Trie;
root->init();
scanf("%d",&nm);
while(nm--){
scanf("%s",var);
BuildTrie(var,root);
}
BuildFail(root);
scanf("%s",stt);
int len= change(stt,stmp);
res=Query(stmp,root);
Reverse(stmp,len);
res+=Query(stmp,root);
printf("%d\n",res);
}
return ;
}
hdu ----3695 Computer Virus on Planet Pandora (ac自动机)的更多相关文章
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...
- HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...
- HDU3695 - Computer Virus on Planet Pandora(AC自动机)
题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
随机推荐
- iOS多线程GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...
- 简明python教程 --C++程序员的视角(二):函数及作用域
函数通过def关键字定义.def关键字后跟一个函数的标识符名称,然后跟一对圆括号.圆括号之中可以包括一些变量名,该行以冒号结尾.接下来是一块语句,它们是函数体. 函数的基本概念 1 位置:从左往右进行 ...
- 0523 Scrum 项目6.0
团队名称:√3 团队目标:全力完成这次的项目 团队口号:我要改变世界,改变自己!!! 演讲稿:我们的产品 鸡汤精选 是为了解决 当下社会出现的太多的负能量使得人们的内心十分 的痛苦, 他们需要强大的正 ...
- ASP标准控件的重要性
1.BackColor 属性:用于显示ListBox控件中的文本和图形的背景颜色,默认为白色(Window) 2.BorderStyle 属性:控制在列表框ListBox周围绘制的边框的类型,其枚举值 ...
- 【android Studio】零git知识、零脚本命令,即刻体验git版本管理魅力!
git的优点就不去多说了.阻碍咱新手体验它的唯一问题就是门槛太高,脚本看着像天书, 本文主要阐述的,就是如何在android studio上,也能像tfs那样,非常简单的操作,就能使用git进行版本管 ...
- Xcode7编译打包后,iOS9设备无法打开http网址的问题
在info.plist中添加一个节点: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsAr ...
- iOS 开发笔记-AFNetWorking https SSL认证
一般来讲如果app用了web service , 我们需要防止数据嗅探来保证数据安全.通常的做法是用ssl来连接以防止数据抓包和嗅探 其实这么做的话还是不够的 . 我们还需要防止中间人攻击(不明白的自 ...
- Asp.Net在多线程环境下的状态存储问题
在应用开发中,我们经常需要设置一些上下文(Context)信息,这些上下文信息一般基于当前的会话(Session),比如当前登录用户的个人信息:或者基于当前方法调用栈,比如在同一个调用中涉及的多个层次 ...
- noi 4977 怪盗基德的滑翔翼
题目链接: http://noi.openjudge.cn/ch0206/4977/ LIS http://paste.ubuntu.com/23406594/
- sftp 设置仅能访问自己目录的用户
1. 创建一个目录,owner为root,权限为750或755,此处为 /home/test01 添加一个用户test01,home目录设置为 /home/test01 再创建一个子目录用于用户上传: ...