#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
const int N = 3e6 + 100;
int f[N][100],d[N];
int n, q,t,cnt=1,ans=0;
char s[N];
void insert(char *s)
{
int len = strlen(s), p = 1, x;
for (int i = 0; i < len; i++)
{
x = s[i] - '0';
if (!f[p][x])
f[p][x] =++cnt;
p = f[p][x];
d[p]+=1;
}
}
int find(char *s)
{
int ans = 0, p=1;
int len = strlen(s);
for (int i = 0; i <len; i++)
{
int x = s[i] - '0';
if (!f[p][x])
return 0;
p = f[p][x];
}
return d[p];
}
int main() {
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
for (int i = 1; i <= cnt; i++)
{
d[i] = 0;
for (int j = 0; j <= 85; j++)
f[i][j] = 0;
}
cnt = 1;
for (int i = 1; i <= n; i++)
{
scanf("%s", s);
insert(s);
}
for (int i =1 ; i <= q; i++)
{
scanf("%s", s);
ans=find(s);
printf("%d\n", ans);
}
}
return 0;
}
字典树模板
最新推荐文章于 2024-09-10 15:28:21 发布