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): 3391    Accepted Submission(s): 918


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



题意:给你n个目标串,问你文本串中出现多少个目标串以及它的反串(原串反过来)。


醉了,一个地方TLE那么多次 o(╯□╰)o


思路:解析文本串,正反各查询一次。注意每次查询过程中,把已经查询的节点赋值为-1,在下次查询时碰到就停止查找。


AC代码:702ms

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 250000+100
using namespace std;
struct Trie
{
    int next[MAXN][30], fail[MAXN], word[MAXN];
    int root, L;
    int newnode()
    {
        for(int i = 0; i < 26; i++)
            next[L][i] = -1;
        word[L++] = 0;
        return L-1;
    }
    void init(){
        L = 0;
        root = newnode();
    }
    void Insert(char *s)
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0; i < len; i++)
        {
            if(next[now][s[i]-'A'] == -1)
                next[now][s[i]-'A'] = newnode();
            now = next[now][s[i]-'A'];
        }
        word[now]++;
    }
    void Build()
    {
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < 26; i++)
        {
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 26; i++)
            {
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    Q.push(next[now][i]);
                }
            }
        }
    }
    int Query(char *s)
    {
        int len = strlen(s);
        int now = root;
        int ans = 0; int temp;
        for(int i = 0; i < len; i++)
        {
            now = next[now][s[i]-'A'];
            temp = now;
            while(temp != root && word[temp] != -1)
            {
                ans += word[temp];
                word[temp] = -1;
                temp = fail[temp];
            }
        }
        now = root;
        for(int i = len-1; i >= 0; i--)
        {
            now = next[now][s[i]-'A'];
            temp = now;
            while(temp != root && word[temp] != -1)
            {
                ans += word[temp];
                word[temp] = -1;
                temp = fail[temp];
            }
        }
        return ans;
    }
};
Trie ac;
char s[1010];
char str[5100000+10];
char ss[5100000+10];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        ac.init();
        for(int i = 0; i < n; i++)
            scanf("%s", s), ac.Insert(s);
        ac.Build();
        scanf("%s", str);
        int len = strlen(str);
        int k = 0;
        for(int i = 0; i < len; i++)
        {
            if(str[i] == '[')
            {
                i++;
                int num = 0;
                while(str[i] < 'A' || str[i] > 'Z')
                {
                    num = num * 10 + str[i] - '0';
                    i++;
                }
                while(num--)
                    ss[k++] = str[i];
                i++;
            }
            else
                ss[k++] = str[i];
        }
        ss[k] = '\0';
        printf("%d\n", ac.Query(ss));
    }
    return 0;
}


你可能感兴趣的:(hdoj 3695 Computer Virus on Planet Pandora 【AC自动机】)