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;
    }
    
  • 相关阅读:
    deepin 安装微信与QQ
    安装git
    在ubuntu上安装最新稳定版本的node及npm
    如何恢复Initial commit之前的源文件
    git使用之错误分析及解决(持续更新)
    【翻译】在Ext JS 5应用程序中如何使用路由
    【翻译】Sencha Ext JS 5发布
    【翻译】Ext JS 4——Ajax和Rest代理处理服务器端一场和消息的方法
    【翻译】Ext JS——高效的编码风格指南
    【翻译】在Ext JS 5种使用ViewControllers
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11631916.html
Copyright © 2011-2022 走看看