Trie树,是一种树形结构。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较
AcWing 835. Trie字符串统计
维护一个字符串集合,支持两种操作:
“I x”向集合中插入一个字符串x;
“Q x”询问一个字符串在集合中出现了多少次。
共有N个操作,输入的字符串总长度不超过 105,字符串仅包含小写英文字母。
输入格式
第一行包含整数N,表示操作数。
接下来N行,每行包含一个操作指令,指令为”I x”或”Q x”中的一种。
输出格式
对于每个询问指令”Q x”,都要输出一个整数作为结果,表示x在集合中出现的次数。每个结果占一行(1≤N≤2∗1e4)
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define endl '\n'
#include<algorithm>
using namespace std;
inline ll read()
{
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1; ch=getchar(); }
while('0'<=ch&&ch<='9') x=x*10+ch-'0', ch=getchar();
return f*x;
}
const int N = 1e5+5;
int son[N][26],idx,cnt[N]; //cnt记录出现次数
//因为只有26个小写字母,所以第二维开26大小
void insert(string str)
{
int p=0;//0既是根节点,也代表空节点
for(int i=0;i<str.length();i++)
{
int u=str[i]-'a';
if(!son[p][u]) son[p][u]=++idx;//如果节点p处没有u这个分支,就创建一个
p=son[p][u];//以儿子为父亲,继续向下走
}
cnt[p]++;//走到结尾后记录一下在该处结尾的字符串数量
}
int query(string str)
{
int p=0;//从根节点开始查找
for(int i=0;i<str.length();i++)
{
int u=str[i]-'a';
if(!son[p][u]) return 0;//如果查到中途发现无路可走说明没有这个字符串
p=son[p][u];
}
return cnt[p];//走到结尾后返回在该处结尾的字符串数量
}
int main()
{
int n; cin>>n;
char op; string x;
while(n--)
{
cin>>op>>x;
if(op=='I') insert(x);
else cout<<query(x)<<endl;
}
return 0;
}
例题:
AcWing 143. 最大异或对
在给定的N个整数a1,a2……aN中选出两个进行xor(异或)运算,得到的结果最大是多少?
输入格式
第一行输入一个整数N。
第二行输入N个整数a1~an。
(1≤N≤1e5, 0≤ai<2^31)
输出格式
输出一个整数表示答案。
做法:
因为n的最大范围是1e5,暴力枚举的O(n^2)做法肯定超时,这里就用到了Trie树优化
因为ai<=2^31,所以一个数的二进制最长有31位,我们可以把每个数的二进制用Trie树存起来,又由异或的性质知道,要想两个数的异或值最大,应该尽量使这两个数的二进制数的每一位都不相同,当然优先使高位不相同(因为异或会使得相同为0,不同为1)
所以对于一个数ai,我们可以根据它的二进制数表示,从最高位31开始,在Trie树中找与当前位不同的数,就这样从最高位到最低位,每次都走与ai当前位数相反的方向,比如ai当前的第x位为0,我们就向1的方向走,否则向0的方向走,前提是该方向存在
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#define ll long long
#define ull unsigned long long
#define up_b upper_bound
#define low_b lower_bound
#define m_p make_pair
#define mem(a) memset(a,0,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define endl '\n'
#include<algorithm>
using namespace std;
inline ll read()
{
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1; ch=getchar(); }
while('0'<=ch&&ch<='9') x=x*10+ch-'0', ch=getchar();
return f*x;
}
const int N = 1e5+5;
const int M = 3e6+5;
//因为每个数最多有31位,最多有1e5个数,极端情况会有3e6个节点
int n,a[N];
int son[M][2],idx;
void insert(int x) //插入的过程也是构建Trie树的过程
{
int p=0;//从根节点0开始
for(int i=30;~i;i--)//从高位到低位插入
{
int u=x>>i&1;
if(!son[p][u]) son[p][u]=++idx;//如果当前节点没有该分支,就在此处创建一个分支
p=son[p][u]; //以p的儿子为父亲继续向下走
}
}
int query(int x)
{
int p=0,res=0;
for(int i=30;~i;i--)
{
int u=x>>i&1;
if(son[p][!u]) //如果与s相反的方向存在就向相反的方向走
{
res+=1<<i; //对答案的贡献
p=son[p][!u];
}
else p=son[p][u]; //不存在只能往本身的方向走
}
return res;
}
int main()
{
n=read();
for(int i=0;i<n;i++)
{
a[i]=read();
insert(a[i]);//插入到Trie树中
}
int ans=0;
for(int i=0;i<n;i++) ans=max(ans,query(a[i]));
cout<<ans<<endl;
return 0;
}