HDU 3065 病毒侵袭持续中 (AC自动机)

Problem Description

小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?


Input

第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。


Output

按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。


Sample Input

3
AA
BB
CC
ooxxCC%dAAAoen….END


Sample Output

AA: 2
CC: 1


Hint

题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。
计数策略也可一定程度上从Sample中推测。


Solution

复习一下AC自动机的板子。
直接写AC自动机(注意简单AC机写法和写trie图的区别)
还有匹配函数好久没写了

最后此题坑点就是要用gets()读入,因为空格也是可能出现的。。


Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define N 1111
#define MAXL 55
#define LL 2000005

using namespace std; 

int n, cur, L, Ans[N];
char x[N][MAXL], s[LL];

struct Trie{
    Trie *son[30], *fail;
    int cnt;
    void Clear(){
      for(int i = 0; i < 26; i++)  son[i] = NULL;
      cnt = 0;
    }
};

Trie Node[N*MAXL], *q[N*MAXL], *Root;

Trie *NewTnode(){
    Node[cur].Clear();
    return Node+cur++;
}

void Trie_Insert(char *x, int num){
    int len = strlen(x);
    Trie *now = Root;
    for(int i = 0; i < len; i++){
      int pos = x[i] - 'A';
      if(!now->son[pos])  now->son[pos] = NewTnode();
      now = now->son[pos];    
    }
    now->cnt = num;
}


void Trie_Build(){

    int head = 0, tail = 0;
    q[0] = Root;
    Root->fail = NULL;

    Trie *now = Root, *temp;
    while(head <= tail){
      now = q[head++];
      for(int i = 0; i < 26; i++)  if(now->son[i]){
        q[++tail] = now->son[i];
        now->son[i]->fail = Root;
        temp = now->fail;
        while(temp){
          if(temp->son[i]){
            now->son[i]->fail = temp->son[i];
            break;
          }
          temp = temp->fail;
        }
      }
    }
}

void Trie_Find(){

    int len = strlen(s);
    Trie *now = Root, *temp;
    for(int i = 0; i < len; i++){
      if(s[i] < 'A' || s[i] > 'Z'){  
        now = Root; //指针回溯
        continue;
      } 
      int pos = s[i] - 'A';
      while(!now->son[pos] && now != Root)  now = now->fail;
      if(!now->son[pos])  continue;
      now = now->son[pos];
      temp = now;
      while(temp){
        Ans[temp->cnt] ++;
        temp = temp->fail;
      }
    }
}
int main(){

    while(~ scanf("%d", &n)){

      cur = 0;
      memset(Ans, 0, sizeof(Ans));
      Root = NewTnode();
      for(int i = 1; i <= n; i++){
        scanf("%s", &x[i]);
        Trie_Insert(x[i], i);
      }

      Trie_Build();

      gets(s);
      gets(s);

      Trie_Find();
      for(int i = 1; i <= n; i++)
        if(Ans[i])
        printf("%s: %d\n", x[i], Ans[i]);
    }
    return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值