zoukankan      html  css  js  c++  java
  • HDU

    链接:

    https://vjudge.net/problem/HDU-2222

    题意:

    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.

    思路:

    自动机模板

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int MAXN = 2e6+10;
    
    struct AcTree
    {
        int Next[26];
        int fail;
        int end;
        void Init()
        {
            memset(Next, 0, sizeof(Next));
            end = 0;
            fail = 0;
        }
    }tree[MAXN];
    char s[MAXN];
    int n, cnt;
    
    void Insert(char *s)
    {
        int len = strlen(s);
        int pos = 0;
        for (int i = 0;i < len;i++)
        {
            if (tree[pos].Next[s[i]-'a'] == 0)
                tree[pos].Next[s[i]-'a'] = ++cnt, tree[cnt].Init();
            pos = tree[pos].Next[s[i]-'a'];
        }
        tree[pos].end++;
    }
    
    void BuildAC()
    {
        queue<int> que;
        for (int i = 0;i < 26;i++)
        {
            if (tree[0].Next[i] != 0)
            {
                tree[tree[0].Next[i]].fail = 0;
                que.push(tree[0].Next[i]);
            }
        }
        while (!que.empty())
        {
            int u = que.front();
            que.pop();
            for (int i = 0;i < 26;i++)
            {
                if (tree[u].Next[i] != 0)
                {
                    tree[tree[u].Next[i]].fail = tree[tree[u].fail].Next[i];
                    que.push(tree[u].Next[i]);
                }
                else
                    tree[u].Next[i] = tree[tree[u].fail].Next[i];
            }
        }
    }
    
    int Query(char *s)
    {
        int len = strlen(s);
        int pos = 0, cnt = 0;
        for (int i = 0;i < len;i++)
        {
            pos = tree[pos].Next[s[i]-'a'];
            for (int t = pos;t != 0 && tree[t].end != 0;t = tree[t].fail)
            {
                cnt += tree[t].end;
                tree[t].end = 0;
            }
        }
        return cnt;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            cnt = 0;
            tree[0].Init();
            scanf("%d", &n);
            for (int i = 1;i <= n;i++)
            {
                scanf("%s", s);
                Insert(s);
            }
            BuildAC();
            scanf("%s", s);
            printf("%d
    ", Query(s));
        }
    
        return 0;
    }
    
  • 相关阅读:
    求逆序对
    2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
    江西财经大学第一届程序设计竞赛
    EOJ Monthly 2018.4
    AtCoder Regular Contest 096
    “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)
    Wannafly挑战赛14
    是男人就过 8 题--Pony.AI A AStringGame
    埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛
    第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11631916.html
Copyright © 2011-2022 走看看