hdu 2896AC自动机

//
//  main.cpp
//  AC自动机
//
//  Created by liuzhe on 16/11/16.
//  Copyright © 2016年 my_code. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

struct Trie
{
    int next[210*500][128],fail[210*500],end[210*500];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 128;i++)
            next[L][i] = -1;
        end[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char s[],int id)
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][s[i]] == -1)
                next[now][s[i]] = newnode();
            now=next[now][s[i]];
        }
        end[now]=id;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 128;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 128;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    bool used[510];
    bool query(char buf[],int n,int id)
    {
        int len = strlen(buf);
        int now = root;
        memset(used,false,sizeof(used));
        bool flag = false;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]];
            int temp = now;
            while(temp != root)
            {
                if(end[temp] != -1)
                {
                    used[end[temp]] = true;
                    flag = true;
                }
                temp = fail[temp];
            }
        }
        if(!flag)return false;
        printf("web %d:",id);
        for(int i = 1;i <= n;i++)
            if(used[i])
                printf(" %d",i);
        printf("\n");
        return true;
    }
};
char buf[10010];
Trie ac;
int main()
{
    int n,m;
    while(scanf("%d",&n) != EOF)
    {
        ac.init();
        for(int i = 1;i <= n;i++)
        {
            scanf("%s",buf);
            ac.insert(buf,i);
        }
        ac.build();
        int ans = 0;
        scanf("%d",&m);
        for(int i = 1;i <= m;i++)
        {
            scanf("%s",buf);
            if(ac.query(buf,n,i))
                ans++;
        }
        printf("total: %d\n",ans);
    }
    return 0;
}



点击打开链接
题意:给了病毒编号,又给了网站,问哪些网站中了病毒,并将中的病毒编号输出,最后输出共有多少网站中病毒
思路:AC自动机模版题,将结构体里的num记为病毒编号就行了,我的代码如果将N设为128就会超内存,但是可见的ASCII只有32到127,不用计算空格,94就可以了
[html] view plain copy
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
typedef long long ll;  
const int inf=0x3f3f3f3f;  
const int maxn=5000010;  
const int N=94;  
struct node{  
    node *fail;  
    node *next[N];  
    int num;  
    node(){  
        fail=NULL;  
        num=0;  
        memset(next,NULL,sizeof(next));  
    }  
}*q[maxn];  
char str2[10003];  
bool vis[503];  
int head,tail;  
void insert_Trie(char *str,node *root,int cn){  
    node *p=root;  
    int i=0;  
    while(str[i]){  
        int id=str[i]-' ';  
        if(p->next[id]==NULL) p->next[id]=new node();  
        p=p->next[id];i++;  
    }  
    p->num=cn;  
}  
void build_ac(node *root){  
    root->fail=NULL;  
    q[head++]=root;  
    while(head!=tail){  
        node *temp=q[tail++];  
        node *p=NULL;  
        for(int i=0;i<N;i++){  
            if(temp->next[i]!=NULL){  
                if(temp==root) temp->next[i]->fail=root;  
                else{  
                    p=temp->fail;  
                    while(p!=NULL){  
                        if(p->next[i]!=NULL){  
                            temp->next[i]->fail=p->next[i];  
                            break;  
                        }  
                        p=p->fail;  
                    }  
                    if(p==NULL) temp->next[i]->fail=root;  
                }  
                q[head++]=temp->next[i];  
            }  
        }  
    }  
}  
int query(node *root){  
    int i=0,ans=0;  
    node *p=root;  
    while(str2[i]){  
        int id=str2[i]-' ';  
        while(p->next[id]==NULL&&p!=root) p=p->fail;  
        p=p->next[id];  
        p=(p==NULL)?root:p;  
        node *temp=p;  
        while(temp!=root){  
            vis[temp->num]=1;  
            if(temp->num!=0) ans++;  
            temp=temp->fail;  
        }  
        i++;  
    }  
    return ans;  
}  
int main(){  
    int n,m;  
    while(scanf("%d",&n)!=-1){  
        node *root=new node();  
        head=tail=0;  
        for(int i=0;i<n;i++){  
            scanf("%s",str2);  
            insert_Trie(str2,root,i+1);  
        }  
        build_ac(root);  
        scanf("%d",&m);  
        int sum=0;  
        for(int i=1;i<=m;i++){  
            scanf("%s",str2);  
            memset(vis,0,sizeof(vis));  
            int tt=query(root);  
            if(tt){  
                sum++;  
                printf("web %d:",i);  
                for(int j=1;j<=n;j++){  
                    if(vis[j]) printf(" %d",j);  
                }  
                printf("\n");  
            }  
        }  
        printf("total: %d\n",sum);  
    }  
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值