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): 3894    Accepted Submission(s): 1038


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

题意:匹配模式串,问有多少个模式串可以被匹配上,将文章反转后可匹配上也算。

裸的AC自动机,把文章正着匹配一遍,倒着匹配一遍。

简直想打自己。。上一次敲模板是小写字母,这次换了大写字母没改,样例死活过不去,简直。。。

写个博客压压惊,提醒一下自己,模板记得要改了啦!!!

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
const int M = 1005;
const int SIZE = 30;
const int N = 5e6 + 10;

char str[M];
int tricnt; //统计结点数
int ch[N][SIZE]; //存结点
int fails[N];
int ed[N];  
bool vis[N]; //当前模式串有没有访问过
char txt1[N + N]; //输入文本
char txt2[N + N];

int newnode()
{
  memset(ch[tricnt], 0, sizeof(ch[tricnt]));
  fails[tricnt] = 0;
  ed[tricnt] = 0;
  vis[tricnt] = false;
  return tricnt++;
}

void init()
{
  tricnt = 0;
  newnode();
}

void trinsert(char* s)
{
  int u = 0;
  int len = strlen(s);
  int v;
  for (int i = 0; i < len; i++) {
    v = s[i] - 'A';
    if (!ch[u][v])
      ch[u][v] = newnode();
    u = ch[u][v];
  }
  ed[u]++;
}

void getfail()
{
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (ch[0][i])
      q.push(ch[0][i]);

  while (!q.empty()) {
    int r = q.front();
    q.pop();
    for (int i = 0; i < 26; i++) {
      int v = ch[r][i];
      if (v) {
        q.push(v);
        int u = fails[r];
        while (u && !ch[u][i])
          u = fails[u];
        fails[v] = ch[u][i];
      }
    }
  }
}

int finds(char* s)
{
  getfail();
  int tot = 0;
  int u = 0;
  int p, v;
  for (int i = 0; s[i]; i++) {
    v = s[i] - 'A';
    while (u && !ch[u][v]) u = fails[u];
    u = ch[u][v];
    p = u;
    while (p && !vis[p]) {
      tot += ed[p];
      vis[p] = true;
      p = fails[p];
    }
  }
  return tot;
}

int main()
{
  int cascnt;
  cin >> cascnt;
  while (cascnt--) {
    int n;
    scanf("%d", &n);
    init();
    for (int i = 0; i < n; i++) {
      scanf("%s", str);
      trinsert(str);
    }
    scanf("%s", txt1);
    int len = strlen(txt1);
    int cnt = 0;
    for (int i = 0; i < len; i++) {
      if (txt1[i] >= 'A' && txt1[i] <= 'Z') {
        txt2[cnt++] = txt1[i];
      }
      else {
        i++;
        int sumcnt = 0;
        while (txt1[i] >= '0' && txt1[i] <= '9') {
          sumcnt = sumcnt * 10 + txt1[i] - '0';
          i++;
        }
        while (sumcnt--) {
          txt2[cnt++] = txt1[i];
        }
        i++;
      }
    }
    txt2[cnt] = '\0'; //构建字符串不加\0也会wa,嗯。
    for (int i = 0; i < cnt; i++) {
      txt1[i] = txt2[i];
    }
    int ans = finds(txt2);
    for (int i = 0; i < cnt; i++) {
      txt2[cnt - i - 1] = txt1[i];
    }
    ans += finds(txt2);
    printf("%d\n", ans);
  }
  return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值