求助trie模板
  • 板块学术版
  • 楼主juruojjl_
  • 当前回复9
  • 已保存回复9
  • 发布时间2022/11/21 19:00
  • 上次更新2023/10/27 02:03:05
查看原帖
求助trie模板
164836
juruojjl_楼主2022/11/21 19:00
#include<bits/stdc++.h>
using namespace std;
int Map[3000005][66],T,n,q,cur;
int exist[3000005];
int turn(char c)
{
    if(c>='A'&&c<='Z') return c-'A';
    else if(c>='a'&&c<='z') return c-'a'+26;
    else return c-'0'+52;
}
void insert(char s[])
{
	int root=0,len=strlen(s);
	for(int i=0;i<len;i++)
	{
		if(!Map[root][turn(s[i])]) Map[root][turn(s[i])]=++cur;
		root=Map[root][turn(s[i])];
		exist[root]++;
	}
}
int find(char s[])
{
	int root=0,len=strlen(s);
	for(int i=0;i<len;i++)
	{
		if(!Map[root][turn(s[i])]) return 0;
		root=Map[root][turn(s[i])];
	}
	return exist[root];
}
int main()
{
	cin>>T;
	for(int cnt=1;cnt<=T;cnt++)
	{
		memset(Map,0,sizeof(Map));
		memset(exist,false,sizeof(exist));
		cin>>n>>q;
		cur=0;
		char str[3141592];
		for(int i=1;i<=n;i++) scanf("%s",str),insert(str);
		for(int i=1;i<=q;i++) scanf("%s",str),cout<<find(str)<<endl;
	}
}

tle

2022/11/21 19:00
加载中...