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.
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.
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
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 = ;
char a[N] , str[];
int k = , tree[][];
int color[];
int fail[];
int ans = ;
void winsert(char *a)
{
int p = ;
for(int i = ; 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 = ; i < ; i++)
{
if(tree[][i])
{
fail[tree[][i]] = ;
q.push(tree[][i]);
}
}
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i = ; i < ; 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 = ;
for(int i = ; str[i] ; i++)
{
now = tree[now][str[i] - 'a'];
for(int j = now ; j && color[j] != - ; j = fail[j])
{
ans += color[j] ;
color[j] = - ;
}
} } int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
memset(tree, ,sizeof(tree));
memset(color , , sizeof(color));
ans = ;
k = ;
int n ;
scanf("%d" , &n);
for(int i = ; i < n ; i++)
{
scanf("%s" , a);
winsert(a);
}
fail[] = ;
getfail();
scanf("%s" , str);
query(str);
printf("%d\n" , ans);
} return ;
}