我汤姆又来灌水区问题啦!
  • 板块灌水区
  • 楼主jzy_go
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/11/30 08:03
  • 上次更新2023/10/27 00:57:37
查看原帖
我汤姆又来灌水区问题啦!
47205
jzy_go楼主2022/11/30 08:03

P8306 【模板】字典树

评测结果五颜六色啥都有,这是为什么

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
	long long x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+(ch-'0');
		ch=getchar();
	}
	return x*f;
}
int exist[3000001];
int nex[3000001][65], cnt;
int getnum(char ch)
{
	if(ch>='a'&&ch<='z')
	return ch-'a';
	if(ch>='A'&&ch<='Z')
	return ch-'A'+26;
	return ch-'0'+52;
}
struct trie {
	  // 该结点结尾的字符串是否存在
	void insert(string s, int l) 
	{  // 插入字符串
		int p = 0;
		for (int i = 0; i < l; i++) 
		{
			int c = getnum(s[i]);
			if (!nex[p][c]) nex[p][c] = ++cnt;  // 如果没有,就添加结点
			p = nex[p][c];
			exist[p] ++;
		}
	}
	int find(string s, int l) 
	{  // 查找字符串
		int p = 0;
		for (int i = 0; i < l; i++) 
		{
		    int c = getnum(s[i]);
		    if (!nex[p][c]) return 0;
		    p = nex[p][c];
		}
		return exist[p];
	}
};
char ch[100001];
int main()
{
	ios::sync_with_stdio(false);
	int t=read();
while(t--)
{
	int n=read(),m=read();
	trie tr;
	for(int i=0;i<=cnt;i++)
        for(int j=0;j<=64;j++)
            nex[i][j]=0;
    for(int i=0;i<=cnt;i++)
        exist[i]=0;
	string s;
	cnt=0;
	for(int i=1;i<=n;i++)
	{
		cin>>s;	
		int len=s.size();
		tr.insert(s,len);
	}
	for(int i=1;i<=m;i++)
	{
		cin>>s;	
		int len=s.size();
		cout<<tr.find(s,len)<<endl;
	}
}
	return 0;
}
/*
1
1 1
998244353
9
*/
2022/11/30 08:03
加载中...