首页 > 代码库 > hdu2222-- Keywords Search(AC自动机入门)

hdu2222-- Keywords Search(AC自动机入门)

Keywords Search
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

 

Appoint description: 

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

15shehesayshrheryasherhs
 

Sample Output

3
 

入门的问题,给出n个字符串,问结果字符串可以匹配到几个给出的字符串。

自动机的模板题,留作模板了

 

 

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std ;struct node{    int flag ;    node *next[26] , *fail ;};queue <node*> que ;char str[1100000] , s[100] ;node *newnode(){    node *p ;    p = new node ;    p->flag = 0 ; p->fail = NULL ;    for(int i = 0 ; i < 26 ; i++)        p->next[i] = NULL ;    return p ;}void settree(char *s,node *p){    int i , k , l = strlen(s) ;    for(i = 0 ; i < l ; i++)    {        k = s[i] - 'a' ;        if( p->next[k] == NULL )            p->next[k] = newnode() ;        p = p->next[k] ;    }    p->flag++ ;    return ;}void setfail(node *rt){    int i ;    node *p , *q ;    while( !que.empty() ) que.pop() ;    que.push(rt) ;    while( !que.empty() )    {        p = que.front() ;        que.pop() ;        for(i = 0 ; i < 26 ; i++)        {            if( p->next[i] )            {                q = p->fail ;                while( q && !q->next[i] )                    q = q->fail ;                p->next[i]->fail = q == NULL ? rt : q->next[i] ;                que.push(p->next[i]) ;            }            else                p->next[i] = p == rt ? rt : p->fail->next[i] ;        }    }}int query(char *str,node *rt){    int num = 0 , i , k , l = strlen(str) ;    node *p = rt , *temp ;    for(i = 0 ; i < l ; i++)    {        k = str[i] - 'a' ;        p = p->next[k] ;        temp = p ;        while( temp && temp->flag )        {            num += temp->flag ;            temp->flag = 0 ;            temp = temp->fail ;        }    }    return num ;}int main(){    int t , n , i ;    node *rt ;    scanf("%d", &t) ;    while( t-- )    {        rt = newnode() ;        scanf("%d", &n) ;        while( n-- )        {            scanf("%s", s) ;            settree(s,rt) ;        }        setfail(rt) ;        scanf("%s", str) ;        printf("%d\n", query(str,rt)) ;    }    return 0;}

hdu2222-- Keywords Search(AC自动机入门)