zoukankan      html  css  js  c++  java
  • 【hdu2222】Keywords Search AC自动机

    题目描述

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
    Wiskey also wants to bring this feature to his image retrieval system.
    Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
    To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

    输入

    First line will contain one integer means how many cases will follow by.
    Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
    Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
    The last line is the description, and the length will be not longer than 1000000.

    输出

    Print how many keywords are contained in the description.

    样例输入

    1 5

    she

    he

    say

    shr

    her

    yasherhs

    样例输出

    3


    题目大意

    给定你几个字串和一个匹配串,问有多少个字串在匹配串中出现过

    题解

    裸的AC自动机,不用指针也能过。

    需要注意的是匹配到之后需要将cnt改为0,防止重复计算。

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    queue<int> q;
    int nt[250001][26] , fail[250001] , cnt[250001] , tot = 1;
    char str[1000001] , w[51];
    void build()
    {
        int i , u , t;
        q.push(1);
        while(!q.empty())
        {
            u = q.front();
            q.pop();
            for(i = 0 ; i < 26 ; i ++ )
            {
                if(nt[u][i])
                {
                    q.push(nt[u][i]);
                    t = fail[u];
                    while(t && !nt[t][i])
                        t = fail[t];
                    if(t) fail[nt[u][i]] = nt[t][i];
                    else fail[nt[u][i]] = 1;
                }
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d" , &T);
        while(T -- )
        {
            memset(nt , 0 , sizeof(nt));
            memset(fail , 0 , sizeof(fail));
            memset(cnt , 0 , sizeof(cnt));
            int n , i , l , u , t , ans = 0;
            scanf("%d" , &n);
            while(n -- )
            {
                scanf("%s" , w);
                l = strlen(w);
                t = 1;
                for(i = 0 ; i < l ; i ++ )
                {
                    if(!nt[t][w[i] - 'a'])
                        nt[t][w[i] - 'a'] = ++tot;
                    t = nt[t][w[i] - 'a'];
                }
                cnt[t] ++ ;
            }
            build();
            scanf("%s" , str);
            l = strlen(str);
            u = 1;
            for(i = 0 ; i < l ; i ++ )
            {
                while(u != 1 && !nt[u][str[i] - 'a'])
                    u = fail[u];
                u = nt[u][str[i] - 'a'];
                if(u == 0)
                    u = 1;
                t = u;
                while(t != 1)
                {
                    if(cnt[t] > 0)
                    {
                        ans += cnt[t];
                        cnt[t] = 0;
                    }
                    else break;
                    t = fail[t];
                }
            }
            printf("%d
    " , ans);
        }
        return 0;
    }
  • 相关阅读:
    链表数据-PHP的实现
    关于go的init函数
    socket小计
    很随笔
    go获取当前项目下所有依赖包
    关于synergy的问题
    二叉树的最大路径和
    大数求和
    重载<<运算符第二个参数必须加上const
    表达式求值
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/6263890.html
Copyright © 2011-2022 走看看