hdu 3065 病毒侵袭持续中

点击打开链接hdu 3065


思路:AC自动机模板
分析:
1 题目要求找出相应的字符串在源码串中出现的次数,并且告诉我们字符串只有大写字母。其实解法是一样的,只是别被大写字母这个给限制了,还是相当于建立trie,但是不能单建立大写字母的trie树。
2 当匹配到一个单词的时候就把单词对应的编号对应的个数加加。

代码


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define MAXN 2000010
#define MAX 1010
#define N 100

int n , cnt;
int vis[MAX];
char words[MAX][N*2];
char text[MAXN];
struct Node{
   Node *next;
   Node *child[N];
   int number;
};
Node node[MAXN];
Node *root;
queue<Node*>q;

Node* newNode(){
   node[cnt].next = NULL;
   node[cnt].number = 0;
   for(int i = 0 ; i < N ; i++)
      node[cnt].child[i] = NULL;
   return &node[cnt++];
}

void insert(char *str , int x){
   int len = strlen(str);
   Node *p = root;

   for(int i = 0 ; i < len ; i++){
      int num = str[i]-32;/*不能只建立大写字母的trie树*/
      if(p->child[num] == NULL)
        p->child[num] = newNode();
      p = p->child[num];
   }
   p->number = x;
}

void getNext(){
    while(!q.empty())
        q.pop();
    
    q.push(root);
    root->next = NULL;
   
    while(!q.empty()){
       Node *p = q.front();
       q.pop();

       for(int i = 0 ; i < N ; i++){
          if(p->child[i]){
             q.push(p->child[i]);
             Node *tmp = p->next;
             while(tmp){
               if(tmp->child[i]){
                 p->child[i]->next = tmp->child[i];
                 break;
               }
               tmp = tmp->next;
             }
             if(tmp == NULL)
                p->child[i]->next = root;
          }
       }    
    }
}

void find(){
   int len = strlen(text);
   Node *p = root;

   for(int i = 0 ; i < len ; i++){
      int num = text[i]-32;
      while(p->child[num] == NULL && p != root)
          p = p->next;
      if(p->child[num]){
        p = p->child[num];
        Node *tmp = p;
        while(tmp){
           if(tmp->number)
              vis[tmp->number]++;/*编号加加*/
           tmp = tmp->next;
        }
      }
   }
}

/*输出*/
void output(){
   for(int i = 1 ; i <= n ; i++){
      if(vis[i])
        printf("%s: %d\n" , words[i] , vis[i]);
   }
}

int main(){
   while(scanf("%d" , &n) != EOF){
       cnt = 0;
       root = newNode();
       memset(vis , 0 , sizeof(vis));
       for(int i = 1 ; i <= n ; i++){
          scanf("%s" , words[i]);
          insert(words[i] , i);
       }
       getchar();
       gets(text);

       getNext();
       find();
       output();
   }
   return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值