HDU2222--Keywords Search

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

 

#include<iostream>
#include<string.h>
using namespace std;
struct node
{
       node*fail;
       node*next[26];
       int cnt;
       node(){
              fail=NULL;
              for( int i =0; i<26; i++)
                   next[i]=NULL;
              cnt=0;
       }            
}* q[500001];
char keyword[51];
char str[1000001];
int first,rear;
void Creat_Trie(char* strg,node*root)  //构造字典树 
{
     int i,id,len;
     node *p=root;
     len=strlen(strg);
     for( i=0; i<len; i++){
          id=strg[i]-'a';
          if( p->next[id]==NULL)
              p->next[id]=new node();
          p=p->next[id];
     } 
     p->cnt++;
     //最后一个结点加1,代表以这个字母结尾的单词有几个。 
}

void Build_ac_automation(node *root)
{
	node * p = NULL;
	node * temp = NULL;
	q[rear++] = root;
	while(first < rear)
	{
		p = q[first++];
		for(int i = 0;i < 26;i++)
		{
			if(p -> next[i] != NULL)
			{
				if(p == root)
				{
					p -> next[i] -> fail = root;
				}
				else 
				{
					temp = p -> fail;
					while(temp != NULL)
					{
						if(temp -> next[i] != NULL)
						{
							p -> next[i] -> fail = temp -> next[i];
							break;
						}
						temp = temp -> fail;
					}
				}
				q[rear++] = p -> next[i];
			}
			else 
			{
				if(p == root)
				{
					p -> next[i] = root;
				}
				else 
				{
					p -> next[i] = p -> fail -> next[i];
				}
			}
		}
	}
}
int Query(node *root) //查询单词 
{
    int i,id,ret,len;
    len=strlen(str);
    node*p=root;
    ret=0;
    for( i=0; i<len; i++){
         id=str[i]-'a';
         p=p->next[id];
         if( p==NULL)
             p=root;
         node *temp=p;
         while( temp!=root&&temp->cnt!=-1){
                ret+=temp->cnt;
                temp->cnt=-1;
                temp=temp->fail;
         }
    }
    return ret;    
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while( t--)
	{
           first=rear=0;
           node *root=new node();
           scanf("%d",&n); 
           getchar();
           while( n--){
                  gets(keyword);
                  Creat_Trie(keyword,root);
           }
           Build_ac_automation(root);
           scanf("%s",str);
           printf("%d\n",Query(root));
    } 
    return 0; 
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值