zoukankan      html  css  js  c++  java
  • HDU 2896 病毒侵袭 AC自动机

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    #define pb(a) push_back(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in.txt","r",stdin);
       // freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    const int SIGMA_SIZE=128;
    struct node
    {
        node *next[SIGMA_SIZE];
        node *fail;
        int val;
        node(){memset(next,0,sizeof(next));fail=NULL;val=0;}
    };
    struct ACAutomation
    {
        node* root;
        void init(){root=new node;}
        int insert(const char *s,const int& v)
        {
            node *p=root;
            for(int i=0;s[i]!='';i++)
            {
                if(p->next[s[i]]!=NULL)p=p->next[s[i]];
                else p=p->next[s[i]]=new node;
            }
            p->val=v;
            return 0;
        }
        int construct()
        {
            root->fail=root;
            queue<node*> q;
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                node *u=root->next[c];
                if(u!=NULL){u->fail=root;q.push(u);}
            }
            while(!q.empty())
            {
                node *r=q.front();q.pop();
                for(int c=0;c<SIGMA_SIZE;c++)
                {
                    node* u=r->next[c];
                    if(u==NULL){r->next[c]=r->fail->next[c];continue;}
                    q.push(u);
                    node* v=r->fail;
                    while(v!=root&&v->next[c]!=NULL)v=v->fail;
                    u->fail=v->next[c];
                }
            }
            return 0;
        }
        void count(node *u,int &num,int *da)
        {
            if(u->val&&!da[u->val])
            {
                num++;
                da[u->val]=1;
                
            }
            if(u->fail!=root&&!da[u->fail->val])count(u->fail,num,da);
        }
        int find(const char *s,int *da)
        {
            int num=0;
            node * u=root;
            for(int i=0;s[i]!='';i++)
            {
                u=u->next[s[i]];
                if(u->val)count(u,num,da);
            }
            return num;
        }
        void free(node *p)
        {
            for(int c='a';c<SIGMA_SIZE;c++)
                if(p->next[c]!=NULL)free(p->next[c]);
            delete p;
        }
    }ac;
    int n,m;
    char T[10010];
    
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            ac.init();
            char s[222];
            for(int i=1;i<=n;i++)
            {
                scanf("%s",s);
                ac.insert(s,i);
            }
            ac.construct();
            scanf("%d",&m);
            int num=0;
            for(int i=1;i<=m;i++)
            {
                scanf("%s",T);
                int da[555];
                memset(da,0,sizeof(da));
                int k=ac.find(T,da);
                if(k>0)
                {
                    printf("web %d:",i);
                    for(int j=1;j<=500;j++)if(da[j])
                        printf(" %d",j);
                    printf("
    ");
                    num++;
                }
            }
            printf("total: %d
    ",num);
           // ac.free(ac.root);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    wamp集成环境安装后无法启动的问题
    jquery点击内层的click事件时会触发外层的click事件
    js 控制文本框只能输入数字
    第七届飞思卡尔智能车比赛的赛道边缘提取第一篇博客
    多级菜单,多级下拉列表解决方案(收藏) 西安
    ASP.NET 从Excel文件导入数据到数据库(笔记) 西安
    动态构造地址栏参数 西安
    我觉得我应该要回来了 西安
    SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转) 西安
    Web.Config 分析 西安
  • 原文地址:https://www.cnblogs.com/BMan/p/3374631.html
Copyright © 2011-2022 走看看