Keywords Search(AC自动机)

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

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<math.h>
#include<set>
#include<time.h>
//#pragma GCC optimize(2)
using namespace std;
//#define INF 1e9
//typedef long long ll;
//typedef unsigned long long ull;
const int allson = 26;
char patten[60], text[1000005];///模式串与文本串
int ans;
struct TireNode
{
    struct TireNode *son[allson];
    struct TireNode *fail;
    int num;///以该节点为结尾的字符串的单词数
}*root;///根节点

/*创建节点*/
TireNode * createNode()
{
    TireNode *p;
    p = (TireNode*)malloc(sizeof(TireNode));/*  malloc():分配内存空间 sizeof(TireNode):node这个变量占用内存的大小
    分配一个TireNode类型大小的内存空间, 并把它赋给TireNode* 型的变量:p  */
    for(int i = 0; i < allson; i++) p->son[i] = NULL;
    p->fail = NULL;
    p->num = 0;
    return p;
}

/*插入模式串,构建字典树*/
void insertPatten()
{
    TireNode *p;
    p = root;
    int index = 0;///模式串的下标
    while(patten[index] != '\0')
    {
        int lowercase = patten[index] - 'a';
        if(p->son[lowercase] == NULL)
            p->son[lowercase] = createNode();
        p = p->son[lowercase];
        index++;
    }
    p ->num++;
}

/*构建AC自动机,构造fail指针*/
void build_AC_automaton()
{
    TireNode *p;
    p = root;
    queue<TireNode*>Q;
    Q.push(p);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        for(int i = 0; i < allson; i++)
        {
            if(p->son[i] != NULL)
            {
                if(p == root)
                    p->son[i]->fail = root;
                else
                {
                    TireNode *temp = p->fail;
                    while(temp != NULL)
                    {
                        if(temp->son[i] != NULL)
                        {
                            p->son[i]->fail = temp->son[i];
                            break;
                        }
                        temp = temp->fail;
                    }
                    if(temp == NULL)
                        p->son[i]->fail = root;
                }
                Q.push(p->son[i]);
            }
        }
    }
}

/*遍历文本串,查看匹配数*/
void find_AC_automaton()
{
    TireNode *p;
    p = root;
    int index = 0;
    while(text[index] != '\0')
    {
        int lowercase = text[index] - 'a';
        while(p->son[lowercase] == NULL && p != root)///失配之后返回fail指针,看是否有匹配
            p = p->fail;
        p = p->son[lowercase];
        if(p == NULL) p = root;
        TireNode *temp = p;
        while(temp != NULL && temp ->num != 0)
        {
            ans += temp->num;
            temp->num = 0;
            temp = temp->fail;
        }
        index++;
    }
}

/*释放指针内存*/
void freeNode(TireNode *node)
{
    if(node != NULL)
    {
        for(int i = 0; i < allson; i++)
            freeNode(node->son[i]);
    }
    free(node);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        root = createNode();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",patten);
            insertPatten();
        }
        build_AC_automaton();
        scanf("%s",text);
        ans = 0;
        find_AC_automaton();
        printf("%d\n",ans);
        freeNode(root);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值