ac自动机(tree+kmp模板)

Keywords Search

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


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
没什么好说的:上模板
https://blog.csdn.net/bestsort/article/details/82947639
https://www.cnblogs.com/cjyyb/p/7196308.html
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
using namespace std;
const int N = 100009 ;
char a[N] , str[1000009];
int k = 0 , tree[500009][27];
int color[500009];
int fail[500009];
int ans = 0 ;
void winsert(char *a)
{
    int p = 0 ;
    for(int i = 0 ; a[i] ; i++)
    {
        int c = a[i] - 'a';
        if(!tree[p][c]) tree[p][c] = ++k;
        p = tree[p][c];
    }
    color[p]++;
}

void getfail()
{
    queue<int>q;
    for(int i = 0 ; i < 26 ; i++)
    {
        if(tree[0][i])
        {
            fail[tree[0][i]] = 0;
            q.push(tree[0][i]);
        }
    }
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        for(int i = 0 ; i < 26 ; i++)
        {
            if(tree[now][i])
            {
                fail[tree[now][i]] = tree[fail[now]][i];
                q.push(tree[now][i]);
            }
            else
            {
                tree[now][i] = tree[fail[now]][i];
            }
        }
    }

}

void query(char *str)
{
    int now = 0 ;
    for(int i = 0 ; str[i] ; i++)
    {
        now = tree[now][str[i] - 'a'];
        for(int j = now ; j && color[j] != -1 ; j = fail[j])
        {
            ans += color[j] ;
            color[j] = -1 ;
        }
    }

}

int main()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        memset(tree, 0  ,sizeof(tree));
        memset(color , 0 , sizeof(color));
        ans = 0 ;
        k = 0 ;
        int n ;
        scanf("%d" , &n);
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%s" , a);
            winsert(a);
        }
        fail[0] = 0;
        getfail();
        scanf("%s" , str);
        query(str);
        printf("%d
" , ans);
    }


    return 0 ;
}
原文地址:https://www.cnblogs.com/nonames/p/11360650.html