Keywords Search


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



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


 题目大意:求取给定串能匹配到的关键字的个数
题目分析:ac自动机裸题

#define MAX 500001
#include <cstring>
#include <cstdio>
#include <iostream>

using namespace std;

const int kind = 26;

struct Node
{
    Node* fail;
    Node* next[kind];
    int count;
    Node ()
    {
        fail = NULL;
        count = 0;
        for ( int i = 0 ; i < kind ; i++ )
            next[i] = NULL;
    }
} *q[MAX];

char str[MAX<<1];
int head,tail;

void insert ( Node * root )
{
    Node *p = root;
    int i = 0 , index;
    while ( str[i] )
    {
        index = str[i]-'a';
        if ( p->next[index] == NULL ) p->next[index] = new Node ();
        p = p->next[index];
        i++;
    }
    p->count++;
}

void build ( Node* root )
{
    root->fail = NULL;
    q[head++] = root;
    while ( head != tail )
    {
        Node *temp = q[tail++];
        Node *p = NULL;
        for ( int  i = 0 ; i < 26 ; i++ )
        {
            if ( temp->next[i] != NULL )
            {
                if ( temp == root ) temp->next[i]->fail = root;
                else
                {
                    p = temp->fail;
                    while ( p != NULL )
                    {
                        if ( p->next[i] != NULL )
                        {
                            temp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if ( p == NULL ) temp->next[i]->fail = root;    
                }
                q[head++] = temp->next[i];
            }
        }
    }
}

int query ( Node *root )
{
    int i = 0 , cnt = 0 , index;
    Node *p = root;
    while ( str[i] )
    {
        index = str[i] - 'a';
        while ( p->next[index] == NULL && p != root ) 
            p = p->fail;
        p = p->next[index];
        p = (p==NULL)?root:p;
        Node * temp = p;
        while ( temp != root && temp ->count != -1 )
        {
            cnt += temp->count;
            temp->count = -1;
            temp = temp->fail;
        } 
        i++;
    }
    return cnt;
}

int t,n;

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        head = tail = 0;
        scanf ( "%d" , &n );
        Node * root = new Node ( );
        while ( n-- )
        {
            scanf ( "%s" , str );
            insert ( root );
        }
        build ( root );
        scanf ( "%s" , str );
        printf ( "%d\n" , query ( root ) ); 
    }
}