Keywords Search(AC自动机-结构体数组)

Keywords Search

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自动机模板题;

推荐视频:UESTCACM 每周算法讲堂 AC自动机

模板代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=1000006;
const int maxm=500005;
struct Aho
{
    struct state
    {
        int ne[26]; //
        int fail; //失配指针
        int cnt; //这一点有多少个串结束
    } stateT[maxm];

    int tot;  //结点个数

    queue<int> qu;

    void init()  //初始化
    {
        while(!qu.empty())
            qu.pop();
        for(int i=0; i<maxm; i++)
        {
            mem(stateT[i].ne,0);
            stateT[i].fail=stateT[i].cnt=0;
        }
        tot=1;
    }

    void Insert(char *str)  //插入字符串
    {
        int ls=strlen(str);
        int rt=0;
        for(int i=0; i<ls; i++)
        {
            int x=str[i]-'a';   //把字符串的字符存入字典树
            if(!stateT[rt].ne[x])
                stateT[rt].ne[x]=tot++;  //若这一字符没有在这一分支 ,节点数+1;
            rt=stateT[rt].ne[x];
        }
        stateT[rt].cnt++;  //经过这一节点的++;
     //   printf("stateT_cnt==%d\n",stateT[rt].cnt);
    }

    void Build()  //失配指针,若失配要找到它的父亲,进行宽搜(queue)
    {
        stateT[0].fail=-1;  //根节点指向-1
        qu.push(0);
        while(!qu.empty())
        {
            int u=qu.front();
            qu.pop();
            for(int i=0; i<26; i++)  //提出一个点
            {
                if(stateT[u].ne[i]) //如果有
                {
                    if(u==0)  //当它是根节点时指向0;
                        stateT[stateT[u].ne[i]].fail=0;
                    else   //没有,就找祖先
                    {
                        int v=stateT[u].fail;  
                        while(v!=-1)   //一直找 直到祖先有i这条边
                        {
                            if(stateT[v].ne[i])
                            {
                                stateT[stateT[u].ne[i]].fail=stateT[v].ne[i];
                                break;  //保证找到的是最长的
                            }
                            v=stateT[v].fail;
                        }
                        if(v==-1)  //没找到
                            stateT[stateT[u].ne[i]].fail=0;
                    }
                    qu.push(stateT[u].ne[i]);
                }
            }
        }
    }

    int Get(int x)  //得到这个数(加自己)的祖先的个数
    {
        int sum=0;
        while(x)
        {
            sum=sum+stateT[x].cnt;
            stateT[x].cnt=0;
            x=stateT[x].fail;
        }
       // printf("Gsum=%d\n",sum);
        return sum;
    }
    int Match(char *str)
    {
        int ls=strlen(str);
        int ans=0,rt=0;
        for(int i=0; i<ls; i++)  //找的是以i为后缀有几个匹配的字符串
        {
            int x=str[i]-'a';
            if(stateT[rt].ne[x])
                rt=stateT[rt].ne[x];
            else  //找祖先
            {
                int f=stateT[rt].fail;

                while(f!=-1 && stateT[f].ne[x]==0) //所指数没有值
                    f=stateT[f].fail;
                if(f==-1)  //始终没找到,回到根节点
                    rt=0;
                else
                    rt=stateT[f].ne[x];
            }
            if(stateT[rt].cnt)  //如果这个点还有串没匹配
              {
                  ans=ans+Get(rt); //rt所有祖先cnt的和。
              }
        }
        return ans;
    }

} aho;
int n;
char S[maxn];
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        aho.init();  //初始化
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%s",S);
            aho.Insert(S);  //插入字典树
        }
        aho.Build();  //建AC自动机的树
        scanf("%s",S);
        printf("%d\n",aho.Match(S));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值