#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Trie
{
unordered_map<char,Trie*> child;
int end=0,ans=0;
void free(Trie* x)
{
for (pair<char,Trie*> i:x->child)
{
free(i.second);
}
delete x;
}
Trie():end(0),ans(0){}
~Trie()
{
free(this);
}
};
Trie* root;
void insert(string s)
{
Trie* temp=root;
for (int i=0;i<s.size();i++)
{
if (temp->child.find(s[i])==temp->child.end())
{
temp->child[s[i]]=new Trie();
}
temp=temp->child[s[i]];
}
temp->end++;
return;
}
int sum(Trie* t)
{
if (t==nullptr)
{
return 0;
}
int res=t->end;
for (pair<char,Trie*> i:t->child)
{
res+=sum(i.second);
}
t->ans=res;
return res;
}
int query(string s)
{
Trie* temp=root;
for (int i=0;i<s.size();i++)
{
if (temp->child.find(s[i])==temp->child.end())
{
return 0;
}
temp=temp->child[s[i]];
}
return temp->ans;
}
int t;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>t;
while (t--)
{
int n,q;
cin>>n>>q;
root=new Trie();
for (int i=1;i<=n;i++)
{
string s;
cin>>s;
insert(s);
}
sum(root);
for (int i=1;i<=q;i++)
{
string s;
cin>>s;
cout<<query(s)<<'\n';
}
}
return 0;
}剪贴板
剪贴板 jrdbn2hf
剪贴板信息
剪贴板信息
- 收录
- 更新