★【字符串匹配】Keywords Search

Problem Description
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.

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output
Print how many keywords are contained in the description.
 

Sample Input
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

用AC自动机实现字符串匹配。

首先利用给定的若干个单词,建立起一棵Trie树,然后用这棵Trie树来建立一个AC自动机,最后用AC自动机来统计匹配结果即可。
Accode:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

const int maxL = 60;
const int SIZE = 0x7ffff;
const int maxLEN = 1000010;

struct AC_auto
{
    AC_auto *next[26], *Fail;
	//next指针即为各个字母(共26个字母);
	//当匹配不成功时,Fail指针用于跳转到
	//Trie树中的另一节点。
    int cnt;
    AC_auto(): Fail(NULL), cnt(0)
    {memset(next, 0, sizeof next);}
};

AC_auto *q[SIZE + 1]; //用队列辅助建立AC自动机。
char key[maxL], str[maxLEN];
int n, T, f, r;

inline void insert(AC_auto *&root, const char *str)
{
    AC_auto *p = root;
    for (int i = 0; str[i]; ++i)
    {
        int Index = str[i] - 'a';
        if (!(p -> next[Index]))
            p -> next[Index] = new AC_auto();
        p = p -> next[Index];
    }
    ++(p -> cnt); //用于记录到此为止的单词数目。
    return;
}
//将字符串str插入到Trie树中,
//这棵树从根到叶(也可能是到中间的某个字母)
//的每一条路径上的字母为一个单词。

inline void build(AC_auto *&root)
{
    root -> Fail = NULL; //根节点的失败指针为空。
    f = r = 0;
    for (q[r++] = root; f < r;)
    {
        AC_auto *Now = q[f++], *p = NULL;
        for (int i = 0; i < 26; ++i)
        if (Now -> next[i]) //若当前节点有这棵子树,则遍历。
        {
            if (Now == root)
                Now -> next[i] -> Fail = root;
	//根的各个直接子节点的失败指针为根。
            else for (p = Now -> Fail; p; p = p -> Fail)
            if (p -> next[i])
            {
                Now -> next[i] -> Fail = p -> next[i];
                break;
            }
	//找当前节点的其它失败指针中是否存在该字母,
	//若有,则把当前节点的子树的失败指针指向之。
            if (!p) Now -> next[i] -> Fail = root;
	//若没找到,则失败指针为根。
            q[r++] = Now -> next[i];
	//将这棵子树入队。
        }
    }
    return;
}

inline int query(AC_auto *root, const char *str)
{
    int cnt = 0;
    AC_auto *p = root;
    for (int i = 0; str[i]; ++i)
    {
        int Index = str[i] - 'a';
        while (!(p -> next[Index]) && p != root)
            p = p -> Fail;
	//此路不通时,找它的失败指针。
        p = p -> next[Index];
        p = p ? p : root;
        for (AC_auto *tmp = p; tmp != root &&
             tmp -> cnt != -1; tmp = tmp -> Fail)
        {
            cnt += tmp -> cnt;
            tmp -> cnt = -1; //防止多次统计。
        } //顺便统计其他失败指针中的单词个数。
    }
    return cnt;
}

int main()
{
    freopen("Keywords_Search.in", "r", stdin);
    freopen("Keywords_Search.out", "w", stdout);
    scanf("%d", &T);
    for (; T; --T)
    {
        scanf("%d", &n);
        AC_auto *root = new AC_auto();
        for (; n; --n)
        {
            scanf("%s", key);
            insert(root, key);
        }
        build(root);
        scanf("%s", str);
        printf("%d\n", query(root, str));
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值