hdu 3065 病毒侵袭持续中(AC automaton)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3065

每个子串是不同的,求出在源码串中各个子串出现的次数,按照输入子串的顺序将出现次数大于0的输出。
例如:

Sample Input
  
  
3 AA BB CC ooxxCC%dAAAoen....END
 

Sample Output
  
  
AA: 2 CC: 1
Hint
Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。
AC自动机模板中的insert函数代码p->sum++;统计的是子串出现的次数(可能出现相同的子串,那时sum就大于1了)。本题的子串每个只出现一次,需要找到在源码串中每个子串出现的次数,且要输出对应的子串。所以要建立好字符串和结点的映射关系。输出的sum也要灵活处理。事实上,tag的是否标记就决定了源码串中子串是否可以重叠,标记了则不可以重叠,没有标记则能重叠。话说回来,AC自动机应该是我见过比较耗内存的算法,每次提交都和内存限制有个亲密的接触。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int head,tail;
const int nn=60; //122-65=57
int N;
char str[2000010],keyword[55];  
char s[1000][55];
struct node{
    int sum,id;
    node *next[nn],*fail;
    bool operator <(const node other)const {
        return id>other.id;
    }
    node(){ //inital node
        fail=NULL;
        id=sum=0;
        memset(next,0,sizeof(next)); //here
    }
}*q[55*1000];
node *root;
void insert_t(char str[],int dex){
    int temp,len;
    node *p=root;
    len=strlen(str);
    for(int i=0;i<len;i++){
        temp=str[i]-65;
        if(p->next[temp]==NULL)p->next[temp]=new node();
        p=p->next[temp];
    }
    p->id=dex;
    strcpy(s[dex],str);
    p->sum++;
}
void build_ac(){  //inital fail point
    q[tail++]=root;
    while(head<tail){
        node *p=q[head++],*temp=NULL;
        for(int i=0;i<nn;i++){
            if(p->next[i]!=NULL){
                if(p==root)p->next[i]->fail=root;
                else {
                temp=p->fail;
                while(temp){
                    if(temp->next[i]){
                        p->next[i]->fail=temp->next[i];
                        break;
                    }
                    temp=temp->fail;
                }
                if(!temp)p->next[i]->fail=root;
                }
                q[tail++]=p->next[i];
            }
        }
    }
}
priority_queue<node> que;
void query(){  
    int i,dex,len=strlen(str);
    node *p=root;
    for(i=0;i<len;i++){
        dex=str[i]-65;
        if(dex<0||dex>57){
            p=root;
            continue;
        }
        while(p->next[dex]==NULL&&p!=root)p=p->fail;
        p=p->next[dex];
        if(!p)p=root;
        node *temp=p;
        while(temp!=root&&temp->sum!=0){
            que.push(*temp);
            temp=temp->fail;
        }
    }
}
int main()
{
    int t;
    while(cin>>N){
        head=tail=0;
        root=new node();
        int i,j,total=0;
        for(i=0;i<N;i++){
            scanf("%s",keyword);
            insert_t(keyword,i+1);
        }
        build_ac();
        scanf("%s",str);
        query();
        while(!que.empty()){
            node temp1=que.top();
            que.pop();
            if(!que.empty()){
                node temp2=que.top();
                //que.pop();
                while(temp1.id==temp2.id){
                    temp2.sum+=temp1.sum;  //处理sum
                    temp1=temp2;
                    que.pop();
                    if(!que.empty()){
                        temp2=que.top();
                    }
                    else break;
                }
            }
            printf("%s: %d\n",s[temp1.id],temp1.sum);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值