Computer Virus on Planet Pandora
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1353   Accepted: 256

Description

Aliens on planet Pandora also write computer 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

There are multiple test cases. The first line in the 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.

Output

For each test case, print an integer K in a line meaning that the program is infected by K viruses.

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

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’.

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的更多相关文章

  1. 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 ...

  2. POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)

    题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...

  3. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  5. hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)

    题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...

  6. 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 ...

  7. hdu ----3695 Computer Virus on Planet Pandora (ac自动机)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  8. HDU-3695 Computer Virus on Planet Pandora

    HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...

  9. HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)

    题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...

随机推荐

  1. 解决在开发环境中访问json配置文件时报HTTP 错误 404.3 - Not Found

    问题描述: 在做demo时,使用json文件做配置文件,访问时,http状态提示:404.3错误,经验证json确认存在,路径也没有问题, 在浏览器地址栏里直接访问json时也提示同样的错误, 根据错 ...

  2. mac 使用apache开启https功能,实现ios局域网内测(一)

    笔者尝试了网上各种办法最后还是找到了方法解决了一系列局域网内测的问题,随手做个笔记记录下,亲测可行. 一.先生成证书 1.进入apache web 的根目录处理证书命令 cd /Library/Web ...

  3. FastDFS这种架构,如何配置?

    FastDFS这种架构,如何配置?才能让欧洲用户可以就近下载Storage Server1的文件,而不是到中国的Storage Server 2下载?

  4. WPF自定义控件之水印文本(密码)框

    首先来讲讲创建这个控件的初衷,一个让我很郁闷的问题. 公司的客户端项目采用WPF+MVVM技术实现,在近期地推客户端的过程中遇到了一个很奇葩的问题:在登录界面点击密码框就会直接闪退,没有任何提示 密码 ...

  5. mysql事务块处理

    begin ,rollback,commit .当然有的人用begin /begin work .推荐用START TRANSACTION 是SQL-99标准启动一个事务. start transac ...

  6. ASP.NET绑定CHECKBOXLIST--------JQUERY绑定CLICK事件,获取CHECKBOX的VALUE和显示值

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...

  7. Android中Context的总结及其用法

    在android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个t ...

  8. MVC页面跳转,路径重复的问题

    window.location.replace("../Home/xxx") 这是js路径跳转的示范,如果普通超链接也一样 前面加一个../

  9. JDK1.9-新特性

    1. Java平台级模块系统 该特性使Java9最大的一个特性,Java提供该功能的主要的动机在于,减少内存的开销,JVM启动的时候,至少会有30~60MB的内存加载,主要原因是JVM需要加载rt.j ...

  10. 多线程-wait/notify/notifyAll

    引言 在Java中,可以通过配合调用Object对象的wait,notify和notifyAll来实现线程间的通信. 在线程中调用wait方法,将阻塞带带其他线程的通知(其他线程调用notify或no ...