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;
    }
    
  • 相关阅读:
    让c像python一样可以在命令行写代码并且编译
    动态链接库找不到 : error while loading shared libraries: libgsl.so.0: cannot open shared object file: No such file or directory
    为什么shell中变量赋值不能有空格
    在hyper安装openwrt
    linux扩展lvm磁盘
    docker的基本使用
    tmux与vim主题不一致
    linux centos cli all proxy
    couchDB入门
    tmux复制到windows剪贴板/粘贴板的坑
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11631916.html
Copyright © 2011-2022 走看看