hdu 2222 Keywords Search(AC自动机入门)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42138    Accepted Submission(s): 13289


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自动机入门题,AC自动机是bfs和dp的经典应用,代码很巧妙。

代码:
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn=1000010;
const int bsz=26;
typedef long long ll;
char txt[1000010];
bool vis[510];
int ans;

struct Trie
{
    int ch[maxn][bsz],val[maxn],sz,cnt[maxn];  // ch存Trie val-节点对应的单词 cnt-节点结尾单词个数
    int f[maxn],last[maxn];//f-失配指针 last-后缀链接
    int newnode()
    {
        val[sz]=0; cnt[sz]=0;
        memset(ch[sz],-1,sizeof ch[sz]);
        return sz++;
    }
    void init()
    {
        sz=0;
        newnode();
    }
    int idx(char c)  // 取c的标号 具体看字符为什么
    {
        return c-'a';
    }
    void Insert(char *st,int id)
    {
        int u=0,n=strlen(st),c,i;
        for(i=0;i<n;i++)
        {
            c=idx(st[i]);
            if(ch[u][c]==-1)
                ch[u][c]=newnode();
            u=ch[u][c];
        }
        val[u]=id;
        cnt[u]++;
    }
    void build()
    {
        int u=0,v,i;
        queue<int> q;
        f[0]=0;
        for(i=0;i<bsz;i++)
        {
            v=ch[u][i];
            if(v==-1) ch[u][i]=0;
            else
            {
                f[v]=0;
                q.push(v);
            }
        }
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            last[u]=val[f[u]]?f[u]:last[f[u]];
            for(i=0;i<bsz;i++)
            {
                v=ch[u][i];
                if(v==-1) ch[u][i]=ch[f[u]][i]; // 将NULL变为有意义 沿着父亲失配指针走第一个有意义的节点
                else
                {
                    f[v]=ch[f[u]][i];
                    q.push(v);
                }
            }
        }
    }
    bool Find(char *st,int m,int id)
    {
        int n=strlen(st),i,u=0,c,p,flag=0;
        //memset(vis,0,sizeof vis);  可标记哪些单词出现过 同样的单词只标记一个
        for(i=0;i<n;i++)
        {
            c=idx(st[i]);
            u=ch[u][c];
            p=val[u]?u:last[u];
            while(p)
            {
                //vis[val[p]]=true;
                if(val[p]){ ans+=cnt[p]; cnt[p]=0; }  // 加上该串出现次数  清零 避免重复计数
                flag=1;
                p=last[p];
            }
        }
        if(!flag) return false;
//        for(i=1;i<=m;i++)
//            if(vis[i])
//                printf(" %d",i);
//        putchar('\n');
        return true;
    }
} ac;
int main()
{
    int i,n,m,tot;
    scanf("%d",&tot);
    while(tot--)
    {
        scanf("%d",&n);
        ac.init();
        for(i=1;i<=n;i++)
        {
            scanf("%s",txt);
            ac.Insert(txt,i);
        }
        ac.build();
        ans=0;
        scanf("%s",txt);
        ac.Find(txt,n,i);
        printf("%d\n",ans);
    }
    return 0;
}
/*
1
5
she
he
say
her
her
yasherhs
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值