#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
const int nodeSize = 5001*6 ;
const int alphaSize = 26 ;
struct Trie{
int root ;
int totel ;
int next[nodeSize][alphaSize] ;
int cnt[nodeSize] ;
int newNode(){
memset(next[totel] , 0 , sizeof(next[totel])) ;
cnt[totel] = 0 ;
return totel++ ;
}
void clear(){
totel = 0 ;
root = newNode() ;
}
void add(char *s){
int now = root ;
for(int i = 0 ; s[i] ; i++){
int son = s[i] - 'a' ;
if(! next[now][son])
next[now][son] = newNode() ;
now = next[now][son] ;
}
cnt[now]++ ;
}
};
Trie trie ;
char dir[10][5] = {"" , "" , "abc" , "def" , "ghi" , "jkl" , "mno" , "pqrs" , "tuv" , "wxyz"} ;
int query(char *s , int now){
if(*s == 0) return trie.cnt[now] ;
int res = 0 ;
int d = *s - '0' ;
for(int i = 0 ; i < strlen(dir[d]) ; i++){
int son = dir[d][i] - 'a' ;
if(! trie.next[now][son]) continue ;
res += query(s+1 , trie.next[now][son]) ;
}
return res ;
}
int main(){
int t , n , m ;
scanf("%d" , &t) ;
while(t--){
scanf("%d%d" , &n , &m) ;
for(int i = 0 ; i < n ; i++) scanf("%s" , ask[i]) ;
trie.clear() ;
while(m--){
scanf("%s" , word) ;
trie.add(word) ;
}
for(int i = 0 ; i < n ; i++) printf("%d\n" , query(ask[i] , trie.root)) ;
}
return 0 ;
}
hdu4287字典树
最新推荐文章于 2021-08-23 23:45:30 发布