• 【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;
    }
  • 相关阅读:
    C# 使用消息队列,包括远程访问
    Python3中urllib使用与源代码
    多年前写的DataTable与实体类的转换
    DataTable添加列和行的三种方法
    DevExpress 常用命令包括导出-打印-打印预览等
    c#开发_Dev的关于XtraGrid的使用(GridControl小结)
    正则表达式精华(包涵常用经典方法)
    数据库 插入时 碰到NULL报错判断的一种方法(技巧)
    MDI窗体简单方法(调用,闪屏)
    GridControl GridView 修改表格中的标题居中
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/6263890.html
Copyright © 2020-2023  润新知