Keywords Search

40 篇文章 0 订阅
28 篇文章 9 订阅


In the modern time, Search engine came into the life of everybody likeGoogle, 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 tofind the image, the system will match the keywords with description ofimage and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and somekeywords, you should tell me how many keywords will be match.

输入描述:
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.
       


输出描述:
Print how many keywords are contained in the description.
示例1

输入

1
5
she
he
say
shr
her
yasherhs

输出

3
#include<iostream>
#include<string>
#include<queue>
using namespace std;
typedef struct node
{
    int count;
    node* fail;
    node* next[26];
    node()
    {
        count=0;
        fail=NULL;
        for(int i=0;i<26;++i)
            next[i]=NULL;
    }
}*Pnode;
Pnode insert(Pnode root,string s)
{
    Pnode p=root;
    int len=s.size();
    for(int i=0;i<len;++i)
    {
        if(!p->next[s[i]-'a'])
            p->next[s[i]-'a']=new node();
        p=p->next[s[i]-'a'];
    }
    p->count++;
    return root;
}
void build_AC(Pnode root)
{
    Pnode p,temp;
    queue<Pnode> q;
    q.push(root);
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        for(int i=0;i<26;++i)
            if(p->next[i])
            {
                if(p==root)
                    p->next[i]->fail=root;
                else
                {
                    temp=p->fail;
                    while(temp)
                    {
                        if(temp->next[i])
                        {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(!temp)
                        p->next[i]->fail=root;
                }
                q.push(p->next[i]);
            }
    }
}
int match(Pnode root,string s)
{
    int result=0,len=s.size();
    Pnode p=root,temp;
    for(int i=0;i<len;++i)
    {
        while(!p->next[s[i]-'a']&&p!=root)
            p=p->fail;
        p=p->next[s[i]-'a'];
        if(!p)
            p=root;
        temp=p;
        while(temp!=root&&temp->count!=-1)
        {
            result+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
    }
    return result;
}
int main()
{
    int t,n;
    string keyword,s;
    Pnode root;
    cin>>t;
    while(t--)
    {
        root=new node();
        cin>>n;
        while(n--)
        {
            cin>>keyword;
            root=insert(root,keyword);
        }
        build_AC(root);
        cin>>s;
        cout<<match(root,s)<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值