Keywords Search(hdu-2222,ac自动机模板题)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 79890    Accepted Submission(s): 27827

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自动机模板题.

fail指针跳到哪里就代表这一点之前的内容已经被匹配了。这样就避免了再从头重复判断的过程

用bfs获取创建fail指针.

 

字典中的单词可能重复,所以要保存数目.

每计算一个单词,我们要删除它,我们用以修改字典中该单词数目来实现

ac:

#include
#include
#include
#include
#include
#define MAXN 1000005
using namespace std;

const int N=10100;
char str[MAXN];
int num,n;
struct node
{
    int son[30];
    int fail,cnt;
}ac[N*60];

queue que;

void init(int x)
{
    ac[x].cnt=0;
    ac[x].fail=0;
    memset(ac[x].son,0,sizeof(ac[x].son));
}

void trie(char ctr[])
{
    int len=strlen(ctr);
    int x=0;
    for(int i=0;i

下面是kuangbin大佬的模板:

#include
#include
#include
using namespace std;
const int N=26;
const int MAXN=500005;
struct Trie{
    int next[MAXN][N],fail[MAXN],end[MAXN];
    int root;
    int tot;
    int newnode()
    {
        for(int i=0;i que;
        fail[root]=root;
        for(int i=0;i

 

你可能感兴趣的:(ac自动机)