HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1353 | Accepted: 256 |
Description
Input
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
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
Source
给你T组测试数据,每组测试数据有n个模式串,后面跟着一个母串,你需要输出母串包含模式串的个数。(反着也算;不计重复串)
【分析】:
模式串构建fail树
母串正着跑一遍匹配,反着跑一遍。(这样既可以降低空间复杂度,又可以提高运行速度)
- Source Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Sz 26
#define m(s) memset(s,0,sizeof s);
using namespace std;
const int N=5e5+5,Z=26,F=5.1e6+5;
int T,n,m,l,ans,cnt=1,tr[N][Z],fail[N],tag[N],q[N];
bool mark[N];char str[F],s[F];
//===============================================
inline void insert(){
int now=1;
for(int i=0,z;i<l;i++){
z=s[i]-'A';
if(!tr[now][z]) tr[now][z]=++cnt;
now=tr[now][z];
}
tag[now]++;
}
inline void acmach(){
for(int i=0;i<Sz;i++) tr[0][i]=1;
int h=0,t=1,now,p;q[t]=1;fail[1]=0;
while(h!=t){
now=q[++h];
for(int i=0;i<Sz;i++){
if(!tr[now][i]) continue;
p=fail[now];
while(!tr[p][i]) p=fail[p];
p=tr[p][i];
fail[tr[now][i]]=p;
q[++t]=tr[now][i];
}
}
}
inline void solve(){
int now=1;l=strlen(s);
for(int z,i=0;i<l;i++){
z=s[i]-'A';
mark[now]=1;
while(!tr[now][z]) now=fail[now];
now=tr[now][z];
if(!mark[now]){
for(int j=now;j;j=fail[j]){
if(tag[j]){
ans+=tag[j];
tag[j]=0;
}
}
}
}
}
//===============Aho Corasick Automata===============
inline int translate(){
int j=0,x=0;
for(int i=0;str[i];i++){
if((str[i]>='A'&&str[i]<='Z')
||(str[i]>='a'&&str[i]<='z')){
s[j++]=str[i];
}
if(str[i]>='0'&&str[i]<='9'){
x=x*10+str[i]-'0';
}
if(str[i]==']'){
while(--x) s[j++]=str[i-1];
}
}
s[j]=0;
return j;
}
inline void Clear(){
ans=0;cnt=1;
m(tr);m(tag);m(fail);m(mark);
}
int main(){
scanf("%d",&T);
while(T--){
Clear();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);l=strlen(s);
insert();
// reverse(s,s+l);
// insert();
}
acmach();
scanf("%s",str);
l=translate();
solve();
reverse(s,s+l);
solve();
printf("%d\n",ans);
}
return 0;
}
HDU 3695 / POJ 3987 Computer Virus on Planet Pandora的更多相关文章
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- 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 ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- 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 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
HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...
- HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...
随机推荐
- GDI+学习笔记(九)带插件的排序算法演示器(MFC中的GDI+实例)
带插件的排序算法演示器 请尊重本人的工作成果,转载请留言.并说明转载地址,谢谢. 地址例如以下: http://blog.csdn.net/fukainankai/article/details/27 ...
- 【SpringMVC学习01】宏观上把握SpringMVC框架
springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...
- 纯CSS弹出层,城市切换效果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- GTD实用指南(转载)
时间管理第一层:记录每日时间开支,认识自己的时间黑洞.你会惊讶地发现,每天的无意义时间很可能在70%以上. [避开时间黑洞的小策略:1.彻底关闭聊天工具:2.关掉邮件的到达提醒功能:3.保持办公环境的 ...
- Spark-shell 无法启动之网络问题
由于需要首次手动安装sbt,需要联网,故将虚拟机的网络适配器模式设置为"桥接模式",这样就可以和互联网相连接. 但是后面执行"spark-shell --master ...
- 函数参数中“x++”造成的运算无效测试
可能以前书上都有说过,当时没在意 只有在实际项目中才会遇到因这个问题导致的Bug 2017/2/26日补充:实际上比较通用的做法是 ++tmp1,这样也可以做到自增 ; ); Console.Writ ...
- 怎样使用Debussy+ModelSim快速查看前仿真波形
引子:ModelSim是HDL仿真软件,Debussy是波形查看软件:搭配使用,相当爽.此处所谓快速查看前仿真波形仅为抛砖引玉,大家不要拘泥于此.两款软件的功能都很强大,请自行研究. 注:本篇博文的软 ...
- Struts2请求流程图
ServletContext中的内容: <s:property value="#attr['countries']['cn']"/> <br> Sessio ...
- 安装yii2高级应用模板
composer global require "fxp/composer-asset-plugin:1.0.0" composer create-project --prefer ...
- 新标准C++程序设计读书笔记_类和对象
面向对象的程序设计方法 抽象:将某类客观事物共同特点(属性)归纳出来,形成一个数据结构(可以用多个变量描述事物的属性):将这类事物所能进行的行为也归纳出来,形成一个个函数,这些函数可以用来操作数据结构 ...