re0分球条
查看原帖
re0分球条
602932
NumberTrart楼主2025/1/17 13:55

甚至本地没有re

#include<iostream>
using namespace std;
int c2i(char c)
{
	if('0'<=c&&c<='9') return c-'0';
	if('a'<=c&&c<='z') return 10+c-'a';
	if('A'<=c&&c<='Z') return 36+c-'A';
}
struct node
{
	char c;
	node* a[64];
	int cnt;
	node(char c='?',int cnt=0):c(c),cnt(cnt){
		for(int i=c2i(0);i<=c2i('Z');i++) a[i]=nullptr;
	}
}*root;
void insert(string str)
{
	//cout<<"str="<<str<<endl;
	node* p=root;
	root->cnt++;
	//cout<<"p="<<p<<"(c="<<p->c<<" cnt="<<p->cnt<<")"<<endl;
	for(int i=0;i<str.size();i++)
	{
		if(p->a[c2i(str[i])]==nullptr) p->a[c2i(str[i])]=new node(str[i]);
		p=p->a[c2i(str[i])];
		p->cnt++;
		//cout<<"p="<<p<<"(c="<<p->c<<" cnt="<<p->cnt<<")"<<endl;
	}
}
int query(string str)
{
	//cout<<"str="<<str<<endl;
	node* p=root;
	//cout<<"p="<<p<<"(c="<<p->c<<" cnt="<<p->cnt<<")"<<endl;
	for(int i=0;i<str.size();i++)
	{
		if(p->a[c2i(str[i])]==nullptr) return 0;
		p=p->a[c2i(str[i])];
		//cout<<"p="<<p<<"(c="<<p->c<<" cnt="<<p->cnt<<")"<<endl;
	}
	return p->cnt;
}
void destroytree(node* p)
{
	if(p==nullptr) return;
	for(int c=c2i('0');c<=c2i('Z');c++) destroytree(p->a[c]);
	delete p;
}
void MAIN()
{
	destroytree(root);
	root=new node;
	int n,q;
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		string str;
		cin>>str;
		insert(str);
	}
	for(int i=1;i<=q;i++)
	{
		string str;
		cin>>str;
		cout<<query(str)<<'\n';
	}
}
int main()
{
	//freopen("C:\\Users\\dell\\Downloads\\P8306_1.in","r",stdin);
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--) MAIN();
	cout<<endl;
	return 0;
}
2025/1/17 13:55
加载中...