字典树
开新坑了,好像是第3个了
#include<cstdio>
#include<string.h>
const int N = 2e6+5;
const int M = 131;
const int INF = 0x3f3f3f3f;
using namespace std;
int T,n,m,Ans,no;
int cnt[N],t[N][M];
char ch[N];
inline int getnum(char K)
{
if(K>='A' && K<='Z') return K - 'A';
else if(K>='a' && K<='z') return K - 'a' + 26;
else return K -'0' + 52;
}
inline void insert(char str[])
{
int p = 0 , len = strlen(str);
for(int i=0;i<len;i++)
{
int c = getnum(str[i]);
if(!t[p][c]) t[p][c] = ++ no;
p = t[p][c];
cnt[p] ++ ;
}
}
inline int Find(char str[])
{
int p = 0 , len = strlen(str);
for(int i=0;i<len;i++)
{
int c = getnum(str[i]);
if(!t[p][c]) return 0;
p = t[p][c];
}
return cnt[p] ;
}
signed main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<=no; i++)
{
cnt[i] = 0 ;
for(int j=0; j<=M; j++)
t[i][j]=0;
}
no = 0;
for(int i=1;i<=n;i++)
{
scanf("%s",ch);
insert(ch);
}
for(int i=1;i<=m;i++)
{
scanf("%s",ch);
printf("%d\n",Find(ch));
}
}
}
凡是过往,皆为序章,前途似海,来日方长
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库