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尽然不会超, ...
随机推荐
- Android自己定义之流式布局
流式布局,优点就是父类布局能够自己主动的推断子孩子是不是须要换行,什么时候须要换行,能够做到网页版的标签的效果. 今天就是简单的做了自己定义的流式布局. 详细效果: 原理: 事实上非常easy,Mea ...
- Android TCP/IP Socket Test
TCP/IP协议:Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议,又名网络通讯协议,是Internet最基本 ...
- vue cli3.0 build 打包 的 js 文件添加版本号 解决 js 缓存问题
在 vue.config.js 的文件中加入下面这段话 // vue.config.jsconst Timestamp = new Date().getTime();module.exports = ...
- python selenium--常用函数3
ActionChains类鼠标操作的常用方法: 引入ActionChains类:from selenium.webdriver.common.action_chains import ActionCh ...
- Spring Aop基础总结
什么是AOP: Aop技术是Spring核心特性之中的一个,定义一个切面.切面上包括一些附加的业务逻辑代码.在程序运行的过程中找到一个切点,把切面放置在此处,程序运行到此处时候会运行切面上的代码.这就 ...
- 李洪强iOS开发之iOS工具收集
李洪强iOS开发之iOS工具收集 项目 简述 日期 我是怎么慢慢变懒的 : Jenkins + 蒲公英 使用Jenkins + 蒲公英使得项目打包给测试人员自动化,大大节省了劳动力 2015.04.1 ...
- jsp中判断对象是否存在
<!-- 如果user对象存在,则显示用户名,如果不存在,则显示空值--> <input type="text" id="userName" ...
- 球队以及得分计算的SQL语句
首先题目是这样的: 球队表teams 比赛表matches 赢了得3分,平局的得1分,输了得0分. 思路: 一个球队的成绩分为两部分,作为主队的得分和作为客队的得分: 计算出一次比赛中具体得了多少分, ...
- 善用php-fpm的慢执行日志slow log,分析php性能问题
众所周知,mysql有slow query log,根据慢查询日志,我们可以知道那些sql语句有性能问题.作为mysql的好搭档,php也有这样的功能.如果你使用php-fpm来管理php的话,你可以 ...
- lnmp集成开发环境安装pdo_dblib扩展
php连接mssql,获取的结果中文乱码,pdo_dblib扩展使用的是apt-get install php5-sybase方法安装的,尝试了修改freetds.conf php.ini 文件编码 ...