首页 > 其他 > 详细

HDU - 2222-Keywords Search(AC自动机)

时间:2019-10-07 20:34:54      阅读:101      评论:0      收藏:0      [点我收藏+]

链接:

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\n", Query(s));
    }

    return 0;
}

HDU - 2222-Keywords Search(AC自动机)

原文:https://www.cnblogs.com/YDDDD/p/11631916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!