Keywords Search

TimeLimit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K(Java/Other)

Problem Description

In the moderntime, 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 theimage, the system will match the keywords with description of image and showthe 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 willcontain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and Nkeywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be notlonger than 50.
The last line is the description, and the length will be not longer than1000000.

Output

Print how manykeywords are contained in the description.

Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3
标程:
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
struct node{
       node *fail,*s[27];
       int w;
}*head;
int t,n,i,j,sum;
char temp[51],str[1000001];
queue<node*> myqueue;
node *getfail(node *p,int k){
     if (p->s[k]!=NULL) return p->s[k];
     else
          if (p==head) return head;
          else return getfail(p->fail,k);
}
void built_trie(){
     node *root=head;
     node *tep;
     for (j=0;j<strlen(temp);j++){
         if (root->s[temp[j]-'a']==NULL){
            tep=new node;
            for (int k=0;k<26;k++)
                tep->s[k]=NULL;
            tep->w=0;
            tep->fail=head;
            root->s[temp[j]-'a']=tep;
            }

         root=root->s[temp[j]-'a'];
         if (j==strlen(temp)-1) root->w+=1;
         }
     return ;
}
void built_ac(){
     node *root;
     while (!myqueue.empty()) myqueue.pop();
     myqueue.push(head);
     while (!myqueue.empty()){//构造fail指针用BFS的方法扩展
           root=myqueue.front();
           myqueue.pop();
           for (j=0;j<26;j++)
               if (root->s[j]!=NULL){
                  myqueue.push(root->s[j]);
                  if (root==head) root->s[j]->fail=head;
                  else root->s[j]->fail=getfail(root->fail,j);
                  }
           }
     return ;
}
void find(){
     int len=strlen(str);
     node* tep;
     node *root=head;
     for (j=0;j<len;j++){
         while (root->s[str[j]-'a']==NULL && root!=head) root=root->fail;
         root=(root->s[str[j]-'a']==NULL)?head:root->s[str[j]-'a'];
         tep=root;
         while (tep!=head){//如果单词A出现过了,那么与他具有相同前缀的单词也都出现过了
               sum+=tep->w;
               tep->w=0;
               tep=tep->fail;
               }
         }
     return ;
     }
int main(){
    scanf("%d",&t);
    while (t--){
          sum=0;
          head=new node;
          for (i=0;i<26;i++)
              head->s[i]=NULL;
          head->fail=head;
          head->w=0;
          scanf("%d",&n);
          for (i=0;i<n;i++){
              scanf("%s",temp);
              built_trie();
              }
          built_ac();
          scanf("%s",str);
          find();
          printf("%d\n",sum);
          }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值