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尽然不会超, ...
随机推荐
- C# 调用bat文件
引入名称空间: using System.Diagnostics; Process proc = null; try { string targetDir = string.Format(@" ...
- unity3d通过代码动态创建销毁游戏对象
只能动态创建内部提供的游戏对象,代码如下: //按下C后创建 if (Input.GetKeyDown (KeyCode.C)) { GameObject s1 = GameObject.Create ...
- 纯CSS弹出层,城市切换效果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Hibernate JPA实体继承的映射(二) @MappedSuperclass
基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclass注解将实体类的多个属性分别封装到不同的非实体类中. 1.@MappedSuperclass注解只能标准在类上:@ ...
- Sql添加测试数据
--建测试表 CREATE TABLE T_UserInfo ( Userid varchar(20), UserName varchar(20), RegTime datetime, Tel va ...
- django学习笔记【003】创建第一个带有model的app
[1]python应用程序要连接mysql有多个驱动程序可供选择: 1.MySQLdb 这个只支持python2.x 所以在这里就不说了: 2.mysqlclient 下载地址 https://pyp ...
- RSAProtectedConfigurationProvider加密web.config
上一篇文章介绍了用 DataProtectionConfigurationProvider加密web.config文件的方法,不过他有一个缺陷,加密的文件只有在本机才能解密,如果有多台服务器的话,则需 ...
- Spark-shell 无法启动之网络问题
由于需要首次手动安装sbt,需要联网,故将虚拟机的网络适配器模式设置为"桥接模式",这样就可以和互联网相连接. 但是后面执行"spark-shell --master ...
- 跟着百度学PHP[17]-复习PDO的几个关键词
主要就是复习一下几个关键词.发现太久没有写又忘了.惭愧. final self static const
- impala+hdfs+csv格式文件
[创建目录]hdfs dfs -mkdir -p /user/hdfs/sample_data/csv/devicehdfs dfs -mkdir -p /user/hdfs/sample_data/ ...