跳到正文

个人记录

字典树进阶

yangchunsheng约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月24日
收录
2026年8月26日
更新
2026年11月17日
篇幅
约 8 分钟

版本

  1. v1当前

咯咯咯

【模板】字典树 / Trie

题意+思路

上次的第一题,简要概括下题意和思路

给定n个字符串和q次询问,每次询问给定一个字符串t,求n个字符串里有多少个字符串的前缀为t

记录每个节点被多少个字符串经过,输出t的最后一个字符在树上的位置即可

丑陋代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int a[3000005][65],s[3000005],olo;
int zh(char t)
{
	if(t>='A'&&t<='Z')return t-'A';
	if(t>='a'&&t<='z')return t-'a'+26;
	return t-'0'+26+26;
}
void jr(string t)
{
	int ls=0;
	for(int i=0;i<t.size();i++)
	{
		if(a[ls][zh(t[i])]==0)
		{
			olo++;
			a[ls][zh(t[i])]=olo;
		}
		ls=a[ls][zh(t[i])];
		s[ls]++;
	}
}
int pd(string t)
{
	int ls=0;
	for(int i=0;i<t.size();i++)
	{
		if(a[ls][zh(t[i])]==0)
		{
			olo++;
			a[ls][zh(t[i])]=olo;
		}
		ls=a[ls][zh(t[i])];
	}
	return s[ls];
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n,q;
		cin>>n>>q;
		for(int i=0;i<=olo;i++)
		{
			for(int j=0;j<65;j++)a[i][j]=0;
			s[i]=0;
		}
		olo=0;
		for(int i=1;i<=n;i++)
		{
			string ls;
			cin>>ls;
			jr(ls);
		}
		for(int i=1;i<=q;i++)
		{
			string ls;
			cin>>ls;
			cout<<pd(ls)<<endl;
		}
	}
	return 0;
}

最大异或对

题意+思路

给定n个整数,从中选出两个进行异或,求最大异或值

要求最大异或值,我们自然是希望高位能够为1,因此我们可以从高位开始往低位遍历,找是否有与当前位相反的二进制位

看懂的同学已经看懂了,这就是用字典树找与二进制相反的路径。因此我们可以判断当前位取反后是否有路径,有的话该数字的最大路径的当前位就为1,否则就说明无法异或,该数字的最大路径的当前位就为0,最后将该数字加入字典树,并统计最大路径的值即可

丑陋代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int a[3200005][5],ma=0,olo=0;
void jr(int t)
{
	int ls=0;
	for(int i=31;i>=0;i--)
	{
		if(a[ls][(t>>i)&1]==0)
		{
			olo++;
			a[ls][(t>>i)&1]=olo;
		}
		ls=a[ls][(t>>i)&1];
	}
}
int pd(int t)
{
	int ls=0,ans=0;
	for(int i=31;i>=0;i--)
	{
		if(a[ls][((t>>i)&1)^1])
		{
			ls=a[ls][((t>>i)&1)^1];
			ans+=(1<<i);
		}
		else
		{
			ls=a[ls][(t>>i)&1];
		}
	}
	return ans;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		ma=max(ma,pd(x));
		jr(x);
	}
	cout<<ma;
	return 0;
}

最长异或路径

题意+思路

给定一棵n个节点的带权树,求树中异或路径的最大值

yh[i]表示1到i的异或值

因为i^i=0,0^i=i,所以yh[u]^yh[v]就等于
u到 lca(u,v)的异或值^v到 lca(u,v)的异或值^yh[lca(u,v)]^yh[lca(u,v)]
等于u到v的异或值

先dfs预处理yh的值,再遍历每个点的所有字典树路径,找到最大异或值并输出即可

丑陋代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
struct jt
{
	int v,w;
};
vector<jt>b[100005];
int a[3200005][5],yh[100005],ma=0,olo=0;
void jr(int t)
{
	int ls=0;
	for(int i=30;i>=0;i--)
	{
		if(a[ls][(t>>i)&1]==0)
		{
			olo++;
			a[ls][(t>>i)&1]=olo;
		}
		ls=a[ls][(t>>i)&1];
	}
}
void dfs(int t,int fa)
{
	jr(yh[t]);
	for(jt i:b[t])
	{
		if(i.v==fa)continue;
		yh[i.v]=yh[t]^i.w;
		dfs(i.v,t);
	}
}
int pd(int t)
{
	int ls=0,ans=0;
	for(int i=30;i>=0;i--)
	{
		if(a[ls][((t>>i)&1)^1])
		{
			ls=a[ls][((t>>i)&1)^1];
			ans+=(1<<i);
		}
		else
		{
			ls=a[ls][(t>>i)&1];
		}
	}
	return ans;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		b[u].push_back({v,w});
		b[v].push_back({u,w});
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)ma=max(ma,pd(yh[i]));
	cout<<ma;
	return 0;
}

AT_abc353_e

题意+思路

给定n个字符串,求每个字符串与其他字符串的最长公共前缀长度的和
哇,好水的题

s[i]表示有多少个字符串的路径经过节点i

先统计当前字符串的路径上的所有s[i],再把它加入字典树,可以避免重复统计的情况

将每个字符串求出来的最长公共前缀和加起来并输出即可

丑陋代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int a[3000005][30],s[3000005],ans=0,olo;
int zh(char t)
{
	return t-'a';
}
void jr(string t)
{
	int ls=0;
	for(int i=0;i<t.size();i++)
	{
		if(a[ls][zh(t[i])]==0)
		{
			olo++;
			a[ls][zh(t[i])]=olo;
		}
		ls=a[ls][zh(t[i])];
		s[ls]++;
	}
}
int pd(string t)
{
	int ls=0,sum=0;
	for(int i=0;i<t.size();i++)
	{
		if(a[ls][zh(t[i])]==0)return sum;
		ls=a[ls][zh(t[i])];
		sum+=s[ls];
	}
	return sum;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string ls;
		cin>>ls;
		ans+=pd(ls);
		jr(ls);
	}
	cout<<ans;
	return 0;
}
原文