HDU 3695 Computer Virus on Planet Pandora

Computer Virus on Planet Pandora

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

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
HintIn 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组字符,再给你一个串,问这个串中出现一次上述字符的有哪些,需要注意的串有正向,反向两种。

思路:

我来分析分析坑点,就是在查询的时候要加上End[temp] != -1,否则会TLE,因为在求字符串的时候可能会出现2D之类的,所以要再设一个字符串,所以每次都要初始化,最后这题的数据方位是5100000,所以有些人不太注意的话就成了5e+6 + 10就会太小了。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 5100005;
struct Trie {
    int root, L;
    int Next[maxn][26], fail[maxn], End[maxn];
    int Newnode() {
        for (int i = 0; i < 26; i++) Next[L][i] = -1;
        End[L++] = 0;
        return L - 1;
    }
    void Init() {
        L = 0;
        root = Newnode();
    }
    void Insert(char *buf) {
        int len = strlen(buf);
        int now = root;
        for (int i = 0; i < len; i++) {
            if (Next[now][buf[i] - 'A'] == -1) {
                Next[now][buf[i] - 'A'] = Newnode();
            }
            now = Next[now][buf[i] - 'A'];
        }
        End[now]++;
    }
    void Build() {
        queue 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 *buf) {
        int len = strlen(buf), now = root, sum = 0;
        for (int i = 0; i < len; i++) {
            now = Next[now][buf[i] - 'A'];
            int temp = now;
            while (temp != root && End[temp] != -1) {
                sum += End[temp];
                End[temp] = -1;
                temp = fail[temp];
            }
        }
        return sum;
    }
};
Trie ac;
char buf[maxn], str[maxn];
int main() {
    int t, n;
    scanf("%d", &t);
    while (t--) {
        ac.Init();
        scanf("%d", &n);
        while (n--) {
            scanf("%s", buf);
            ac.Insert(buf);
        }
        ac.Build();
        scanf("%s", buf);
        int len = strlen(buf), cnt = 0;
        memset(str, 0, sizeof(str));
        for (int i = 0; i < len; i++) {
            if (buf[i] != '[') str[cnt++] = buf[i];
            else {
                i++;
                int sum = 0;
                char c;
                while (i < len) {
                    if (isdigit(buf[i])) sum = sum * 10 + buf[i] - '0';
                    else {
                        c = buf[i];
                        break;
                    }
                    i++;
                }
                i++;
                for (int j = 0; j < sum; j++) str[cnt++] = c;
            }
        }
        int ans = ac.Query(str);
        reverse(str, str + cnt);
        ans += ac.Query(str);
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(HDU)