P8306WA求调
  • 板块学术版
  • 楼主chuazen
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/23 19:56
  • 上次更新2024/7/23 19:59:12
查看原帖
P8306WA求调
821073
chuazen楼主2024/7/23 19:56

字典树模板题,52分,不知为何WA

提交记录见https://www.luogu.com.cn/record/168084454

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=3*1e6+5;
int T,n,m,trie[N][65],end_trie[N],total;
char s[N];
bool bo;
int change(char x){
	if(x>='A'&&x<='Z')return int(x-'A');
	else if(x>='a'&&x<='z')return int(x-'a'+26);
	else return int(x-'0'+52); 
}
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d %d",&n,&m);
		for(int i=0;i<=total;i++)for(int j=1;j<=65;j++)trie[i][j]=0;
		for(int i=0;i<=total;i++)end_trie[i]=0;
		total=0;
		for(int i=1;i<=n;i++){
			scanf("%s",s);
			int p=0;
			for(int j=0;j<strlen(s);j++){
				int x=change(s[j]);
				if(trie[p][x]==0)total++,trie[p][x]=total;
				p=trie[p][x];
				end_trie[p]++;
			}
		}
		for(int i=1;i<=m;i++){
			scanf("%s",s);
			int p=0;
			bo=1;
			for(int j=0;j<strlen(s);j++){
				int x=change(s[j]);
				if(trie[p][x]==0){
					bo=0;
					printf("0\n");
					break;
				}
				p=trie[p][x];
			}
			if(bo==1)printf("%d\n",end_trie[p]);
		}
	}
	return 0;
}
2024/7/23 19:56
加载中...