萌新求助
查看原帖
萌新求助
541069
SuperCowHorse楼主2022/8/12 14:20

RT。52pts,WA on #2,3,4。

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e6+5;
struct trie{
	int nex[maxn][63],cnt,tot[maxn];
	bool ex[maxn];
	int to_num(char c){
		if(c>='A'&&c<='Z')
        	return c-'A';
	    if(c>='a'&&c<='z')
	        return c-'a'+26;
	    return c-'0'+52;
	}
	void insert(char *s,int l){
		int p=0;
		for(int i=0;i<l;++i){
			int c=to_num(s[i]);
			if(!nex[p][c])
				nex[p][c]=++cnt;
			p=nex[p][c];++tot[p];
		}
		ex[p]=1;
	}
	int find(char *s,int l){
		int p=0;
		for(int i=0;i<l;++i){
			int c=to_num(s[i]);
			if(!nex[p][c]) return 0;
			p=nex[p][c];
		}
		return tot[p];
	}
	void init(){
		for(int i=1;i<=cnt;++i)
			for(int j=1;j<=62;++j)
				nex[i][j]=ex[i]=0;
		for(int i=1;i<=cnt;++i)
			tot[i]=0;
		cnt=0;
	}
}t;
int T,n,q;
char s[maxn];
int main(){
	scanf("%d",&T);
	while(T--){
		t.init();
		scanf("%d%d",&n,&q);
		for(int i=1;i<=n;++i){
			scanf("%s",s);
			t.insert(s,strlen(s));
		}
		while(q--){
			scanf("%s",s);
			printf("%d\n",t.find(s,strlen(s)));
		}
	}
}
2022/8/12 14:20
加载中...