hdu_2896_病毒侵袭

题目链接

AC自动机。
学习链接:
AC自动机算法
KMP算法&AC自动机

代码君

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <cstdio>
using namespace std;
vector<int>ans;

struct node
{
    int coun;
    node *fail;
    node *next[130];
    node(){
        coun=0;
        fail=NULL;
        memset(next,0,sizeof(next));
    }
};

class Trie
{
private:
    node *root;
public:
    Trie(){
        root=new node();
    }
    ~Trie(){
        del(root);
    }
    void del(node *p){
        if(p==NULL)return;
        for(int i=0;i<130;i++)del(p->next[i]);
        delete p;
    }
    void insert(string s,int x){
        node *p=root;
        int len=s.length();
        for(int i=0;i<len;i++){
            int id=s[i]-31;
            if(p->next[id]==NULL){
                p->next[id]=new node();
            }
            p=p->next[id];
        }
        p->coun=x;
    }
    void build(){
        queue<node*>Q;
        Q.push(root);
        while(!Q.empty()){
            node *p=Q.front();
            Q.pop();
            for(int i=0;i<130;i++){
                if(p->next[i]!=NULL){
                    if(p==root)p->next[i]->fail=root;
                    else{
                        node *tmp=p->fail;
                        while(tmp!=root&&tmp->next[i]==NULL)tmp=tmp->fail;
                        tmp=tmp->next[i];
                        if(tmp==NULL)tmp=root;
                        p->next[i]->fail=tmp;
                    }
                    Q.push(p->next[i]);
                }
            }
        }
    }
    void query(string s){
        node *p=root;
        int len=s.length();
        for(int i=0;i<len;i++){
            int id=s[i]-31;
            while(p!=root && p->next[id]==NULL)p=p->fail;
            p=p->next[id];
            if(p==NULL)p=root;
            node *tmp=p;
            while(tmp!=root){
                if(tmp->coun>=0){
                    // ans+=tmp->coun;
                    if(tmp->coun>0)ans.push_back(tmp->coun);
                    // tmp->coun=-1;
                }else break;
                tmp=tmp->fail;
            }
        }
    }
};

int main(){
    int T;
    int n,n1;
    while(cin>>n){
        string s;
        Trie *ac=new Trie();
        for(int i=0;i<n;i++){
            cin>>s;
            ac->insert(s,i+1);
        }
        ac->build();
        scanf("%d",&n1);
        int tot=0;
        for(int i=0;i<n1;i++){
            cin>>s;
            ans.clear();
            ac->query(s);
            if(ans.size()>0){
                printf("web %d:",i+1);
                sort(ans.begin(),ans.end());
                int t=unique(ans.begin(),ans.end())-ans.begin();
                for(int i=0;i<t;i++)printf(" %d",ans[i]);
                printf("\n");
                tot+=ans.size()>0;
            }
        }
        printf("total: %d\n",tot);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值