社区讨论
全WA,过样例和手搓小数据求调
P8306【模板】字典树参与者 4已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhj0wife
- 此快照首次捕获于
- 2025/11/03 18:54 4 个月前
- 此快照最后确认于
- 2025/11/03 18:54 4 个月前
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll t;
ll n,q;
char ins[3000001];
ll zz;
ll cg(char s)
{
if (s>='A'&&s<='Z')return s-'A'+1;
else if (s>='a'&&s<='z') return s-'a'+27;
else return s-'1'+53;
}
struct Trie
{
ll cnt;
ll sn[71];
}nd[1000001];
inline ll rd()
{
ll ret=0,zf=1;
char ch=getchar();
while (ch>'9'||ch<'1')
{
if (ch=='-')zf=-1;
ch=getchar();
}
while (ch<='9'&&ch>='1')
{
ret=ret*10+ch-'0';
ch=getchar();
}
return ret*zf;
}
void bd(char str[])
{
ll u=0,l=strlen(str),s;
for (int i=0;i<l;++i)
{
s=cg(str[i]);
if (nd[u].sn[s]==0)
{
++zz;
nd[u].sn[s]=zz;
}
u=nd[u].sn[s];
++nd[u].cnt;
}
}
ll fd(char str[])
{
ll u=0,l=strlen(str),s;
for (int i=0;i<l;++i)
{
s=cg(str[i]);
if (nd[u].sn[s]==0)
{
return 0;
}
else u=nd[u].sn[s];
}
return nd[u].cnt;
}
int main()
{
t=rd();
// zz=114514;
for (int i=1;i<=t;++i)
{
for (int j=0;j<=zz;++j)
{
for (int k=0;k<=70;++k)
{
nd[j].sn[k]=0;
}
nd[j].cnt=0;
}
zz=0;
n=rd();
q=rd();
for (int j=1;j<=n;++j)
{
scanf("%s",ins);
bd(ins);
}
for (int j=1;j<=q;++j)
{
scanf("%s",ins);
printf("%d\n",fd(ins));
}
}
return 0;
}
/*
2
5 3
dyy
s1012
hmy
egg
dyyyy
d
yaodan
dyyyy
9 1
e
eg
egg
eggb
eggbr
eggbro
1145141919180
tianyuuu
ez
e
*/
回复
共 5 条回复,欢迎继续交流。
正在加载回复...