HDU3695Computer 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): 2565    Accepted Submission(s): 708


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

对于每个模式串如果只需要记录是否出现一次,建议还对end采用计数的形式,used就超时wa了。。,但是改成query2那样去掉while就A了,不知道为啥

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 250005
#define cha 26
#define TRUE true
#define FALSE false
#define Type int
char articlein[5100005];
char article[5100005];
int ans;
bool used[300];
struct Trie
{
    int next[N][cha];
    int fail[N];
    Type end[N];//用于存放目标节点数据
    int Root, L;
    int newnode()
    {
        for (int i = 0; i < cha; i++)
            next[L][i] = -1;
        end[L] = 0;//数据域
        L++;
        return L - 1;
    }
    void init()
    {
        L = 0;
        Root = newnode();
    }
    void insert(char s[], int id)
    {
        int len = strlen(s);
        int now = Root;
        int ti;
        for (int i = 0; i < len; i++)
        {
            ti = s[i] - 'A'; //ti下标
            if (next[now][ti] == -1)
                next[now][ti] = newnode();
            now = next[now][ti];
        }
        end[now] ++;//数据域
    }
    void build()
    {
        queue<int>Q;
        fail[Root] = Root;
        for (int i = 0; i < cha; 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 < cha; 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]);
                }
            }
        }
    }
    void query(char buf[])
    {
        int len = strlen(buf);
        int now = Root;
        int ti;
        bool flag = false;
        for (int i = 0; i < len; i++)
        {
            ti = buf[i] - 'A'; //ti下标
            now = next[now][ti];
            int temp = now;
            while (temp != Root && end[temp] != -1)
            {
                ans += end[temp];
                end[temp] = -1;
                temp = fail[temp];
            }
        }
    }
    void query2(char buf[])
    {
        int len = strlen(buf);
        int now = Root;
        int ti;
        bool flag = false;
        for (int i = 0; i < len; i++)
        {
            ti = buf[i] - 'A'; //ti下标
            now = next[now][ti];
            int temp = now;
            if (temp != Root) //不知道为啥把循环去掉就过了。。。使用这个需要改成used id的形式
            {
                if (end[temp] != -1 ) //数据域
                {
                    used[end[temp]] = true;
                }
                temp = fail[temp];
            }
        }
    }
    void debug()
    {
        for (int i = 0; i < L; i++)
        {
            printf("id = %3d, fail = %3d, end = %3d, \n", i, fail[i], end[i]);
            // printf("word= [");
            // for(int j=0;j<cha;j++)
            //     printf("%2c", j+'a');
            // printf("]\n");
            printf("chi = [");
            for (int j = 0; j < cha; j++)
            {
                if (next[i][j] == -1)
                    printf("%2c", 'N');
                else
                    printf("%2d", next[i][j]);
            }
            printf("]\n\n");
        }
    }
};
char virs[1005];
Trie ac;
int main()
{
    // freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        ac.init();
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", virs);
            ac.insert(virs, i);
        }
        ac.build();
        scanf("%s", articlein);
        int l = strlen(articlein);
        memset(article, 0, sizeof(article));
        int anum = 0;
        for (int i = 0; i < l; i++)
        {
            if (articlein[i] != '[')
            {
                article[anum++] = articlein[i];
            }
            else
            {
                int de = 0;
                char num[10] = zero;
                int k = 1;
                int nnum = 0;
                for (int j = i + 1; j < l; j++)
                {
                    if (isdigit(articlein[j]))
                    {
                        num[nnum++] = articlein[j];
                    }
                    else
                    {
                        i = j;
                        break;
                    }
                }
                int l = strlen(num);
                for (int d = l - 1; d >= 0; d--)
                {
                    de += k * (num[d] - '0');
                    k *= 10;
                }
                char c = articlein[i];
                i++;
                for (int j = 0; j < de; j++)
                {
                    article[anum++] = c;
                }
            }
        }
        memset(used, 0, sizeof(used));
        // ac.debug();
        ans = 0;
        // cout << article << endl;
        ac.query(article);
        int l2 = strlen(article);
        reverse(article, article + l2);
        // cout << article << endl;
        ac.query(article);
        // for (int i = 0; i < n; i++)
        // {
        //     if (used[i])
        //     {
        //         ans++;
        //         // printf("%d,", i);
        //     }
        // }

        printf("%d\n", ans);

    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 250005
#define cha 26
#define TRUE true
#define FALSE false
#define Type int
char articlein[5100005];
char article[5100005];
int ans;
bool used[300];
struct Trie
{
    int next[N][cha];
    int fail[N];
    Type end[N];//用于存放目标节点数据
    int Root, L;
    int newnode()
    {
        for (int i = 0; i < cha; i++)
            next[L][i] = -1;
        end[L] = -1;//数据域
        L++;
        return L - 1;
    }
    void init()
    {
        L = 0;
        Root = newnode();
    }
    void insert(char s[], int id)
    {
        int len = strlen(s);
        int now = Root;
        int ti;
        for (int i = 0; i < len; i++)
        {
            ti = s[i] - 'A'; //ti下标
            if (next[now][ti] == -1)
                next[now][ti] = newnode();
            now = next[now][ti];
        }
        end[now] = id;//数据域
    }
    void build()
    {
        queue<int>Q;
        fail[Root] = Root;
        for (int i = 0; i < cha; 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 < cha; 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]);
                }
            }
        }
    }
    void query(char buf[])
    {
        int len = strlen(buf);
        int now = Root;
        int ti;
        bool flag = false;
        for (int i = 0; i < len; i++)
        {
            ti = buf[i] - 'A'; //ti下标
            now = next[now][ti];
            int temp = now;
            while (temp != Root)
            {
                if (end[temp] != -1 ) //数据域
                {
                    used[end[temp]] = true;
                }
                temp = fail[temp];
            }
        }
    }
    void query2(char buf[])
    {
        int len = strlen(buf);
        int now = Root;
        int ti;
        bool flag = false;
        for (int i = 0; i < len; i++)
        {
            ti = buf[i] - 'A'; //ti下标
            now = next[now][ti];
            int temp = now;
            if(temp != Root)//不知道为啥把循环去掉就过了。。。
            {
                if (end[temp] != -1 ) //数据域
                {
                    used[end[temp]] = true;
                }
                temp = fail[temp];
            }
        }
    }
    void debug()
    {
        for (int i = 0; i < L; i++)
        {
            printf("id = %3d, fail = %3d, end = %3d, \n", i, fail[i], end[i]);
            // printf("word= [");
            // for(int j=0;j<cha;j++)
            //     printf("%2c", j+'a');
            // printf("]\n");
            printf("chi = [");
            for (int j = 0; j < cha; j++)
            {
                if (next[i][j] == -1)
                    printf("%2c", 'N');
                else
                    printf("%2d", next[i][j]);
            }
            printf("]\n\n");
        }
    }
};
char virs[1005];
Trie ac;
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        ac.init();
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", virs);
            ac.insert(virs, i);
        }
        ac.build();
        scanf("%s", articlein);
        int l = strlen(articlein);
        memset(article, 0, sizeof(article));
        int anum = 0;
        for (int i = 0; i < l; i++)
        {
            if (articlein[i] != '[')
            {
                article[anum++] = articlein[i];
            }
            else
            {
                int de = 0;
                char num[10] = zero;
                int k = 1;
                int nnum = 0;
                for (int j = i + 1; j < l; j++)
                {
                    if (isdigit(articlein[j]))
                    {
                        num[nnum++] = articlein[j];
                    }
                    else
                    {
                        i = j;
                        break;
                    }
                }
                int l = strlen(num);
                for (int d = l - 1; d >= 0; d--)
                {
                    de += k * (num[d] - '0');
                    k *= 10;
                }
                char c = articlein[i];
                i++;
                for (int j = 0; j < de; j++)
                {
                    article[anum++] = c;
                }
            }
        }
        memset(used, 0, sizeof(used));
        // ac.debug();
        ans = 0;
        // cout << article << endl;
        ac.query2(article);
        int l2=strlen(article);
        reverse(article, article + l2);
        // cout << article << endl;
        ac.query2(article);
        for (int i = 0; i < n; i++)
        {
            if (used[i])
            {
                ans++;
                // printf("%d,", i);
            }
        }

        printf("%d\n", ans);

    }

    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值