cxcs @ 2024-01-30 23:17:47
import java.io.*;
import java.util.*;
public class Main {
static int N = 3000010;
static int tree[][];
static int num[]; // 标记拥有该子串的 字符串数量
static int index = 0;
// 映射对应asii
static int getnum(char x){
if(x >= 'A' && x <= 'Z') // 大写
return x - 'A';
else if(x >= 'a'&& x <= 'z') // 小写
return x - 'a' + 26;
else
return x - '0' + 52; // 数字
}
static void insert(char ch[]) {
int id = 0;
for(int i = 0; i < ch.length; i++) {
int c = getnum(ch[i]);
if(tree[id][c] == 0)
tree[id][c] = ++index;
id = tree[id][c];
num[id]++; // 拥有该子串前缀 字符串数量+1
}
}
static int query(char ch[]) {
int id = 0;
for(int i = 0; i < ch.length; i++) {
int c = getnum(ch[i]);
if(tree[id][c] == 0)
return 0;
id = tree[id][c];
}
return num[id];
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
tree = new int[N][65]; // 区分大小写
num = new int[N];
while(T-- > 0) {
String str[] = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int q = Integer.parseInt(str[1]);
for(int i = 0; i < tree.length; i++)
Arrays.fill(tree[i], 0);
Arrays.fill(num, 0);
// 插入
while(n-- > 0)
insert(br.readLine().toCharArray() );
// 查询
while(q-- > 0) {
int res = query(br.readLine().toCharArray() );
bw.write(res + "\n");
}
}
bw.flush();
}
}