• AC自动机-HDU2896-模板题


    http://acm.hdu.edu.cn/showproblem.php?pid=2896

    另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况。

    /*--------------------------------------------------------------------------------------*/
    //        Helica's header 
    //        Second Editions
    //        2015.11.7
    //
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    
    //debug function for a N*M array 
    #define debug_map(N,M,G) printf("
    ");for(int i=0;i<(N);i++)
    {for(int j=0;j<(M);j++){
    printf("%d",G[i][j]);}printf("
    ");}                
    //debug function for int,float,double,etc.
    #define debug_var(X) cout<<#X"="<<X<<endl;
    /*--------------------------------------------------------------------------------------*/
    using namespace std;
    
    int N,M,T;
    int tol;
    
    struct Trie
    {
        int next[100010][100],fail[100010],end[100010];
        int num[100010];
        int root,L;
        int cnt;
        int newnode()
        {
            for(int i=0;i<100;i++)
                next[L][i] = -1;
            end[L++] = 0;
            return L-1;
        }
        void init()
        {
            L = 0;
            root = newnode();
            memset(num,0,sizeof num);
            cnt = 1;
        }
        void insert(char *s)
        {
            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]++;
            num[now] = cnt++;
        }
        void build()
        {
            queue <int> Q;
            fail[root] = root;
            for(int i=0;i<100;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<100;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]);
                    }
                }
            }
        }
        void query(char *s,int no)
        {
            int len = strlen(s);
            int now = root;
            int ans = 0;
            set <int > web;
    
            for(int i=0;i<len;i++)
            {
                now = next[now][s[i]-'!'];
                int temp = now;
                while(temp != root)
                {
                    ans += end[temp];
                    if(end[temp]) web.insert(num[temp]);
                    temp = fail[temp];
                }
            }
            if(!web.empty())
            {
                printf("web %d:",no);
                for(set <int>::iterator it=web.begin();it != web.end();it++)
                    printf(" %d",*it);
                printf("
    ");
                tol++;
            }
        }
    }ac;
    
    char buf[10010];
    
    int main()
    {
        while(~scanf("%d",&N))
        {
            ac.init();
            tol = 0;
            for(int i=0;i<N;i++)
            {
                scanf("%s",buf);
                ac.insert(buf);
            }
            ac.build();
            scanf("%d",&M);
            for(int i=1;i<=M;i++)
            {
                scanf("%s",buf);
                ac.query(buf,i);
            }
            printf("total: %d
    ",tol);
        }
    }
  • 相关阅读:
    C语言使用正则表达式
    linux 有名管道(FIFO)
    sendto() 向广播地址发包返回errno 13, Permission denied错误
    NSUserDefaults(数据存储)
    iOS开发UI篇—UIScrollView控件实现图片缩放功能
    李洪强iOS开发之-环信02_iOS SDK 介绍及导入
    李洪强iOS开发之-环信01_iOS SDK 前的准备工作
    iOS开发UI篇—UIScrollView控件介绍
    iOS开发UI篇—iOS开发中三种简单的动画设置
    iOS开发UI篇—IOS开发中Xcode的一些使用技巧
  • 原文地址:https://www.cnblogs.com/helica/p/5037817.html
Copyright © 2020-2023  润新知