求调
查看原帖
求调
1236701
dtbigwaves楼主2024/7/12 16:09

全MLE 蒟蒻求解

#include<bits/stdc++.h>
using namespace std;
const int N=3e6+10;
int T,t[N][65],cnt[N],idx;
char ch[N],problem[N];
int getnum(char ch){
    if(ch>='A' && ch<='Z') return ch-'A';
    else if(ch>='a' && ch<='z') return ch-'a'+26;
    else return ch-'0'+52;
}
void insert(char str[]){
	 int _=0;
	 for(int i=0;i<strlen(str);i++){
	 	int num=getnum(str[i]);
	 	if(!t[_][num]) t[_][num]=++idx;
	 	_=t[_][num];
	 	cnt[_]++;
	 }
}
int find(char str[]){
	int _=0;
	for(int i=0;i<strlen(str);i++){
		int num=getnum(str[i]);
		if(!t[_][num]) return 0;
		_=t[_][num];
	}
	return cnt[_];
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>T;
	for(int i=1;i<=T;i++){
		int n,q;
		cin>>n>>q;
		memset(t,0,sizeof(t));
		memset(cnt,0,sizeof(cnt));
		idx=0;
		for(int j=1;j<=n;j++){
			scanf("%s",ch);
			insert(ch);
		}
		for(int j=1;j<=q;j++){
			scanf("%s",problem);
			cout<<find(problem)<<endl;
		}
	}
	return 0;
}
2024/7/12 16:09
加载中...