HDU2222 Keywords Search

Keywords Search

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



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
 

Author
Wiskey
 

AC自动机模板。代码有注释

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int kind=26;
struct node
{
    node *fail;
    node *next[kind];
    int count;
    node()
    {
        fail=NULL;
        count=0;
        memset(next,0,sizeof(next));
    }
};
queue<node *> q;
char keyword[55];
char s[1000010];
void insert(char *str,node *root)       //建立字典树
{
    node *p=root;       //以root为根节点
    int i=0,index;
    while(str[i])
    {
        index=str[i]-'a';
        if(p->next[index]==NULL)
            p->next[index]=new node();  //root指向下一个节点,然后下一个节点再建立一个节点
        p=p->next[index];   //p变成下一个节点
        i++;    //字符串右移
    }
    p->count++; //字符串到底才有一个数字
}   //字典树里的节点好像没有存放字符,字符使存放在字典树的边里,就是next
void build_ac_automation(node *root)    //建立ac自动机,弄出fail
{
    int i;
    q.push(root);       //把根节点加入队列
    root->fail=NULL;
    while(!q.empty())   //队列非空
    {
        node *temp=q.front();
        q.pop();
        node *p=NULL;
        for(i=0;i<26;i++)
        {
            if(temp->next[i]!=NULL) //如果temp还有子节点
            {
                if(temp==root)  //如果temp是根节点
                    temp->next[i]->fail=root;   //那么根节点的子节点的fail都是指向根节点
                else
                {
                    p=temp->fail;       //不是根节点,取出这个节点的fail
                    while(p!=NULL)
                    {
                        if(p->next[i]!=NULL)    //不断的寻找p节点的子节点有没有和temp节点相同的元素
                        {
                            temp->next[i]->fail=p->next[i];     //相同的元素指向
                            break;
                        }
                        p=p->fail;  //不断的指向fail知道为空,或者存在元素相同
                    }
                    if(p==NULL) //为空,就指向根节点
                        temp->next[i]->fail=root;
                }
                q.push(temp->next[i]);  //字符存在边里
            }
        }
    }
}
int query(node *root)       //查询操作
{
    int i=0,cnt=0,index,len=strlen(s);
    node *p=root;   //从根节点开始
    while(s[i])
    {
        index=s[i]-'a';
        while(p->next[index]==NULL&&p!=root)    //字符不匹配,沿着失败路径指向的字符继续找
            p=p->fail;
        p=p->next[index];   //下一个字符
        if(p==NULL) //如果为空了,返回根节点
            p=root;
        node *temp=p;
        while(temp!=root&&temp->count!=-1)     //没走过,且不是根节点
        {
            cnt+=temp->count;
            temp->count=-1;
            temp=temp->fail;    //沿失败指针返回继续找
        }
        i++;
    }
    return cnt;
}
int main()
{
    int n,t,i;
    scanf("%d",&t);
    while(t--)
    {
        node *root=new node();
        scanf("%d",&n);
        getchar();
        for(i=0;i<n;i++)
        {
            gets(keyword);
            insert(keyword,root);
        }
        build_ac_automation(root);
        scanf("%s",s);
        printf("%d\n",query(root));
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值