【HDU 2222】 Keywords Search AC自动机

这是一道关于搜索引擎的编程题目,要求在给定的图像描述中匹配关键词。给定一组关键词和一个图像描述,你需要计算有多少关键词出现在描述中。题目限制了关键词和描述的长度,并给出了输入输出样例。解决方案是应用AC自动机算法来解决多个模式串匹配文本串的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 80824    Accepted Submission(s): 28183

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

 题意:就是先给你一个数字,代表有多少组测试数据,然后给你一个数字N,代表有N个模式串,然后分别输入N个模式串和一个文本串,问你文本串中出现N个模式串的次数。

题解:这就是一道多个模式串匹配一个文本串的简单题,直接套用AC自动机即可AC,唯一要注意的就是当输入数据中既有数字又有字符串的时候,一定要吃掉数字和字符串之间的那个换行。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxp=50+7;
const int maxt=1000000+7;
const int maxq=10000*maxp;
struct Trie{                   //利用结构体来封装字典树的结点
    Trie* next[26];          
    Trie* fail;
    int num;
    Trie(){                  //构造函数 便于初始化
        for(int i=0;i<26;i++)
        next[i]=NULL;
        fail=NULL;
        num=0;
    }
};
char P[maxp];
char T[maxt];
Trie* q[maxq];
void insert(Trie* root,char* s)      //在字典树上 插入字符串s
{
    Trie* p=root;
    for(int i=0;s[i]!='\0';i++){
        if(p->next[s[i]-'a']==NULL){
            p->next[s[i]-'a']=new Trie;
        }
        p=p->next[s[i]-'a'];
    }
    p->num++;
}
void build_ac_automation(Trie* root)   //利用BFS创建AC自动机
{
    int head=0,tail=0;
    q[tail++]=root;
    while(head!=tail){
        Trie* front=q[head++];
        for(int i=0;i<26;i++){         //遍历队头元素的子结点
            if(front->next[i]!=NULL){
                Trie* p=front->fail;
                while(p!=NULL){                //只有根结点的失配指针为 NULL
                    if(p->next[i]!=NULL){       ////顺着失配指针往回走,直至某个节点,
                        front->next[i]->fail=p->next[i];//其拥有一个字母为'a'+i的子结点
                        break;
                    }
                    p=p->fail;     // 沿着失配指针一直找
                }
                if(p==NULL) front->next[i]->fail=root; 
                    //p==NULL 说明顺着失配指针往回走的过程中没有找到合适的结点
                q[tail++]=front->next[i];
            }
        }
    }
}
int ac_find(Trie* root,char* T)
{
    int ans=0;
    Trie* p=root;
    for(int i=0,len=strlen(T);i<len;i++){
        while(p->next[T[i]-'a']==NULL&&p!=root)  //若当前结点的没有一个字符为 T[i]的儿子且当前不是根节点 
            p=p->fail;          //通俗的讲,就是顺着失配指针往回走,直至找到合适的节点或根结点为止。
        if(p->next[T[i]-'a']!=NULL) p=p->next[T[i]-'a'];
        Trie* temp=p;
        while(temp!=root&&temp->num!=-1){   //顺着失配指针往回走,一直到根结点。
            ans+=temp->num;//若当前节点的num不为 0,则说明以当前节点字母结尾的单词出现过一次
                             //此单词是以上一次循环的结点单词为结尾的单词的子集。
            temp->num=-1;     //标记 num 为-1,避免重复计算
            temp=temp->fail;
        }
    }
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        Trie* root=new Trie;
        scanf("%d",&n);
        getchar();   //吃掉换行
        for(int i=0;i<n;i++){
            gets(P);
            insert(root,P);
        }
        build_ac_automation(root);
        gets(T);
        printf("%d\n",ac_find(root,T));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值