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尽然不会超, ...
随机推荐
- ASP.NET前后台交互之JSON数据
最近由于项目需要做一个Ajax的搜集单表单的多重数据的需求,所以就采用了前端JQuery保存Object对象之后转换成JSON的数据源传递给后台处理的这样的形式,相信有不少人大多时候是接收后台给出的J ...
- context.Request.Files post 上传问题件
[无刷新上传] 要实现文件上传,form必须设置几个属性:1.action:设为要处理数据的页面地址:2.method:设为"post":3.enctype/encoding:必须 ...
- bashrc,bash_profile和/etc/profile
bashrc,bash_profile和/etc/profile 最近老出现在shell里面能跑的程序用鼠标双击app去不能跑.究其原因是因为环境变量的问题. 在类unix系统中一般有三个bash配置 ...
- 终端I/O termios属性设置 tcsetattr设置(转)
终端I/O有两种不同的工作方式: 规范方式输入处理.在这种方式中,终端输入以行为单位进行处理.对于每个读要求,终端驱动程序最多返回一行. 非规范方式输入处理.输入字符不以行为单位进行装配. 如果不作特 ...
- Reveal使用教程
Reveal使用教程 Reveal是用于透视程序整体结构的一个软件,软件收费89美刀,试用期30天,不过好在有破解版,无需担心花钱的问题 在然后呢,软件在哪下,可以在我的github上下载到破解版本 ...
- JSON学习【转自慕课网】
视频网址 从慕课网视频里的PPT截图过来的,因为是用PHP讲的,而且后面讲的一般,所以只截取了前两节课的基础内容,其实只看一下第一张PPT就可以了.
- 辛星和你彻底搞清CSS中的相对定位和绝对定位
前面我在解读CSS中也说过了关于相对定位和绝对定位的问题.无奈还是有些童鞋表示迷茫,于是另开一篇博客,来具体解读相对定位和绝对定位.希望可以以我的点点星光,让后来者少走弯路. 所谓相对定位,就是设置为 ...
- 用docker搭建测试环境--docker的基本操作
上一篇文章中最后执行了docker pull centos的指令,经过一段时间的等待,会从hub.docker.com上下载docker官方最新的centos的images,接下来熟悉一下docker ...
- hdu3879 Base Station 最大权闭合子图 边权有正有负
/** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...
- struts2 命名空间 namespace 学习
默认的命名空间" namespace="" ". 根命名空间 " namespace="/" ". <packag ...