trie树

tot=1!!!

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n;
int trie[maxn][26],tot=1,ed[maxn*26];
char s[15];
bool add(char str[])
{
	bool f=0;
	int len=strlen(str);
	int p=1;
	for (int i=0;i<len;i++)
	{
		int ch=str[i]-'0';
		if (trie[p][ch] == 0) trie[p][ch]=++tot;
		else if (i == len-1) f=1;
		p=trie[p][ch];
		if (ed[p]) f=1; 
	}
	ed[p]=1;
	return f;
}
//bool check(char str[])
//{
//	int len=strlen(str);
//	int p=1;
//	for (int i=0;i<len;i++)
//	{
//		trie[p][str[i]-'0'];
//		if (p == 0) return 0;
//	}
//	return ed[p];
//}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		memset(trie,0,sizeof(trie));
		memset(ed,0,sizeof(ed));
		tot=1;
		bool flag=0;
		cin >> n;
		while (n--)
		{
			scanf("%s",s);
			if (add(s)) flag=1;
		}
		if (flag) cout << "NO" << endl;
		else cout << "YES" << endl;
	}
	
	return 0;
}
void add(char str[])
{
	int len=strlen(str),p=1;
	for (int i=0;i<len;i++)
	{
		int ch=str[i]-'a';
		if (trie[p][ch] == 0) trie[p][ch]=++tot;
		p=trie[p][ch];
	}
	ed[p]=1;
}

0/1trie*

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+10,maxm=1e5+10;
int n;
int a[maxn];
int trie[maxn][2];
int two[maxm][35];
int tot=1,ed[maxn];
void turn(int x,int n)
{
	for (int i=1;i<=32;i++)
	{
		two[x][32-i]=(n&1);
		n>>=1;
	}
}
void add(int str[])
{
	int len=32,p=1;
	for (int i=0;i<len;i++)
	{
		int ch=str[i];
		if (trie[p][ch] == 0) trie[p][ch]=++tot;
		p=trie[p][ch];
	}
	ed[p]=1;
}
int get(int str[])
{
	int ans=0;
	int len=32,p=1;
	for (int i=0;i<len;i++)
	{
		int ch=str[i]^1;
		if (trie[p][ch]) ans+=(1<<(31-i));
		else ch=ch^1;
		p=trie[p][ch];
	}
	return ans;
}
int main()
{
	cin >> n;
	for (int i=1;i<=n;i++)
	{
		cin >> a[i];
		turn(i,a[i]);
		add(two[i]);
	}
	int ans=0;
	for (int i=1;i<=n;i++) ans=max(ans,get(two[i]));
	cout << ans << endl;
	return 0;
}
posted @ 2026-05-06 15:09  msjing  阅读(12)  评论(0)    收藏  举报