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

Keywords Search

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

Submit Status

Appoint description: 
System Crawler  (2014-05-13)

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 

入门的问题,给出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;
}

时间: 2024-08-29 01:52:45

hdu2222-- Keywords Search(AC自动机入门)的相关文章

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

HDU 2222 Keywords Search (AC自动机入门 模板)

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

hdu2222 Keywords Search ac自动机

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 56558    Accepted Submission(s): 18493 Problem Description In the mo

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

hdu 2222 Keywords Search ac自动机入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串.其中模式串可以重复.问有多少文本串在模式串中出现过.(对于相同的模式串次数仍然累加) 思路:ac自动机裸题: KMP是先将文本串进行匹配得到失配边f[];但是并不适用于文本串较长,模式串较多的情况.因为每次查询的时间复杂度为O(n+m).n,m分别为文本串和模式串的长度: ac自动机就是建立在Trie上,

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

HDU 2222 Keywords Search AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67122    Accepted Submission(s): 22584 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 Keywords Search (AC自动机模板题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik