Ravener @ 2024-06-25 21:07:17
#include <bits/stdc++.h>
#define ll int//awq,想用 long long 但 MLE,不想改了
#define il inline
#define rt return
#define N 3000005
using namespace std;
il ll read()
{
ll x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
rt x*f;
}
il void print(ll a)
{
//if(a<0) putchar('-'),a=-a;
if(a/10) print(a/10);
putchar(a%10+48);
}
struct Trie
{
ll nxt[N][65],cnt[N],cur;
void init()
{
for(ll i=0;i<=cur;i++)
{
for(ll j=0;j<65;j++)
nxt[i][j]=0;
cnt[i]=0;
}
cur=0;
}
void insert(char s[],ll l)
{
ll p=0;
for(ll i=0;i<l;i++)
{
ll c;
if(s[i]<='z'&&s[i]>='a') c=s[i]-'a';
else if(s[i]<='A'&&s[i]>='Z') c=s[i]-'A'+26;
else c=s[i]-'0'+52;
if(!nxt[p][c]) nxt[p][c]=++cur;
p=nxt[p][c];
cnt[p]++;
}
}
ll find(char s[],ll l)
{
ll p=0;
for(ll i=0;i<l;i++)
{
ll c;
if(s[i]<='z'&&s[i]>='a') c=s[i]-'a';
else if(s[i]<='A'&&s[i]>='Z') c=s[i]-'A'+26;
else c=s[i]-'0'+52;
if(!nxt[p][c]) rt 0;
p=nxt[p][c];
}
rt cnt[p];
}
}T;
char s[N];
int main()
{
ll t=read();
while(t--)
{
T.init();
ll n=read(),q=read();
while(n--)
{
cin>>s;
T.insert(s,strlen(s));
}
while(q--)
{
cin>>s;
print(T.find(s,strlen(s)));
putchar('\n');
}
}
}
by Ravener @ 2024-06-26 18:13:23
傻了,条件错了,已结