luqyou @ 2023-02-23 16:18:57
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e6+10;
int t,n,q,trie[maxn][63],cnt,e[maxn];
int id(char c){
if('a'<=c&&c<='z') return c-'a';
else if('A'<=c&&c<='Z') return 26+c-'A';
else return 52+c-'0';
}
void insert(string s){
int len=s.size(),now=0;
s=" "+s;
for(int i=1;i<=len;i++){
if(!trie[now][id(s[i])]){
trie[now][id(s[i])]=++cnt;
}
now=trie[now][id(s[i])];
e[now]++;
}
}
int query(string s){
int len=s.size(),now=0;
s=" "+s;
for(int i=1;i<=len;i++){
if(trie[now][id(s[i])]){
now=trie[now][id(s[i])];
}
else{
return 0;
}
}
return e[now];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
for(int i=1;i<=cnt;i++){
for(int j=1;j<=63;j++){
trie[i][j]=0;
}
}
for(int i=1;i<=cnt;i++){
e[i]=0;
}
cnt=0;
cin>>n>>q;
for(int i=1;i<=n;i++){
string s;
cin>>s;
insert(s);
}
for(int i=1;i<=q;i++){
string s;
cin>>s;
cout<<query(s)<<endl;
}
}
return 0;
}
by vzcx_host @ 2023-02-23 16:26:13
你是不知道string下标从0开始吗
by vzcx_host @ 2023-02-23 16:28:40
另外,为了防止被卡,请换种方式将 trie 数组清 0
by luqyou @ 2023-02-23 16:35:20
@Industrial_banana emm,我写了 s=" "+s
吧
by vzcx_host @ 2023-02-23 16:41:50
看了一下你的提交记录,就是要换种方式将 trie 数组清 0
by luqyou @ 2023-02-23 16:43:48
@Industrial_banana ok
by luqyou @ 2023-02-23 16:45:11
WSSB,忘清0了