HDOJ 题目3695 Computer Virus on Planet Pandora(AC自动机)

Computer Virus on Planet Pandora

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)

Total Submission(s): 3169    Accepted Submission(s): 867

Problem 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

2010 Asia Fuzhou Regional Contest

Recommend

chenyongfu   |   We have carefully selected several similar problems for you:  3699 3692 3691 3698 3697

裸题瞬秒

ac代码

#include<stdio.h>
#include<string.h>
char str[5100000],key[1010],ss[5100000];
int head,tail;
struct node
{
    node *fail;
    node *next[26];
    int cnt;
    node()
    {
        fail=NULL;
        cnt=0;
        for(int i=0;i<26;i++)
            next[i]=NULL;
    }
}*q[252000];
node *root;
void insert(char *s)
{
    int temp,len,i;
    node *p=root;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        temp=s[i]-'A';
        if(p->next[temp]==NULL)
            p->next[temp]=new node();
        p=p->next[temp];
    }
    p->cnt++;
}
void build_ac()
{
    root->fail=NULL;
    q[tail++]=root;
    while(head!=tail)
    {
        node *p=q[head++];
        node *temp=NULL;
        for(int i=0;i<26;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root)
                {
                    p->next[i]->fail=root;
                }
                else
                {
                    temp=p->fail;
                    while(temp!=NULL)
                    {
                        if(temp->next[i]!=NULL)
                        {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL)
                    {
                        p->next[i]->fail=root;
                    }
                }
                q[tail++]=p->next[i];
            }
        }
    }
}
int query()
{
    int ans=0;
    int len=strlen(str);
    node *p=root,*temp;
    for(int i=0;i<len;i++)
    {
        int x=str[i]-'A';
        while(p->next[x]==NULL&&p!=root)
        {
            p=p->fail;
        }
        p=p->next[x];
        if(p==NULL)
        {
            p=root;
        }
        temp=p;
        while(temp!=root&&temp->cnt!=-1)
        {
            ans+=temp->cnt;
            temp->cnt=-1;
            temp=temp->fail;
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int i;
        head=tail=0;
        root=new node();
        for(i=0;i<n;i++)
        {
            scanf("%s",key);
            insert(key);
        }
        build_ac();
        scanf("%s",ss);
        int len=strlen(ss),j=0,k1;
        for(i=0;i<len;)
        {
            if(ss[i]=='[')
            {
                i++;
                int num=0;
                while(ss[i]>='0'&&ss[i]<='9')
                {
                    num=num*10+(ss[i]-'0');
                    i++;
                }
                while(num--)
                {
                    str[j++]=ss[i];
                }
                i+=2;
            }
            else
                str[j++]=ss[i++];
        }
		str[j]='\0';
    //    printf("%s\n",str);
		int ans=query();
		len=strlen(str);
		for(i=0;i<=(len-1)/2;i++)
		{
			char ch=str[len-1-i];
			str[len-1-i]=str[i];
			str[i]=ch;
		}
		ans+=query();
        printf("%d\n",ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 06:38:23

HDOJ 题目3695 Computer Virus on Planet Pandora(AC自动机)的相关文章

hdu 3695 Computer Virus on Planet Pandora(AC自动机)

题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求判断说给定串中包含几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后匹配一次后反转后再匹配一次. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include <algor

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

题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后匹配一次后反转后再匹配一次. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include <algor

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

Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)Total Submission(s): 2578    Accepted Submission(s): 713 Problem Description Aliens on planet Pandora also write computer programs l

HDU 3695 Computer Virus on Planet Pandora (AC自动机)

题意:有n种病毒序列(字符串),一个模式串,问这个字符串包含几种病毒. 包含相反的病毒也算,字符串中[qx]表示有q个x字符.详细见案列. 0 < q <= 5,000,000尽然不会超,无解 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

HDU 3695 Computer Virus on Planet Pandora

Computer Virus on Planet Pandora Problem 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 compute

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

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

hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机

F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3695 Appoint description:  System Crawler  (2014-11-05) Description Aliens on planet Pandora also write comp

18.10.29 POJ 3987 Computer Virus on Planet Pandora(AC自动机+字符串处理)

描述 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

[AC自动机]HDOJ3695 Computer Virus on Planet Pandora

题意:给t.n,t个案例,n个字符串 下面给n+1个字符串,n个互不相同的小串,最后一个是模式串 模式串会出现[qx]的形式,q为数字,x为一个字母 问n个小串在模式串中出现的个数,正着出现.反着出现都算. 蛮裸的ac自动机,本来想着在query里面把找到过的end清零,把模式串展开正着反着找两遍,那即使再找到加零也不影响.但是超时了... 于是把找到过的end标为-1,-1了就不再找下去,就可以了~上代码 #include <cstdio> #include <cstdlib>