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;
    }
    
  • 相关阅读:
    Microsoft Dynamics CRM2011 更换Logo
    Calling LoadLibraryEx on ISAPI filter failed
    Dynamics CRM2013/2015 插件注册工具登录后无法显示assembly列表问题的解决办法二
    python字符串
    python流程控制
    python反射
    python内置函数
    python集合
    python字符编码
    测试appstore地址
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11631916.html
Copyright © 2011-2022 走看看