【蓝桥杯集训10】Tire树 字典树 最大异或对专题(3 / 3)

目录

字典树模板

1、插入操作

2、查询操作

143. 最大异或对 - trie + 二进制

3485. 最大异或和 - 前缀和+Trie+滑动窗口


字典树模板

活动 - AcWing

字典树:高效存储和查找字符串集合的数据结构

  • son[节点1地址][值]=节点2地址 —— 节点1的子节点为节点2
  • cnt[节点地址] —— 记录以该节点地址为结尾的字符串个数

1、插入操作

static void insert(String s)
{
    int p=0;
    for(char x:s.toCharArray())
    {
        int u=x-'a';
        if(son[p][u]==0) son[p][u]=++idx; //如果节点不存在 创建节点
        p=son[p][u]; //走到p的子节点
    }
    cnt[p]++; //把这一串字符插入后 记录以最后此节点为标记的字符串数
}

2、查询操作

static int query(String s)
{
    int p=0;
    for(char x:s.toCharArray())
    {
        int u=x-'a';
        if(son[p][u]==0) return 0; //有一个节点不存在,说明没有这个字符串
        p=son[p][u];
    }
    return cnt[p];
}
/*
    *道阻且长,行则将至*
    author:Roye_ack
*/
import java.util.*;
import java.io.*;
import java.math.*;
 
class Main
{
    static int N=100010;
    static int[] cnt=new int[N];
    static int[][] son=new int[N][26]; //son[节点1][值]=节点2  节点1的子节点是节点2
    static int idx=0;
    
    static void insert(String s)
    {
        int p=0;
        for(char x:s.toCharArray())
        {
            int u=x-'a';
            if(son[p][u]==0) son[p][u]=++idx; //如果节点不存在 创建节点
            p=son[p][u]; //走到p的子节点
        }
        cnt[p]++; //把这一串字符插入后 记录以最后此节点为标记的字符串数
    }
    
    static int query(String s)
    {
        int p=0;
        for(char x:s.toCharArray())
        {
            int u=x-'a';
            if(son[p][u]==0) return 0; //有一个节点不存在,说明没有这个字符串
            p=son[p][u];
        }
        return cnt[p];
    }
    
    public static void main(String[] args) throws IOException
    {
        PrintWriter wt=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        
        int n=rd.nextInt();
        while(n-->0)
        {
            String[] s=rd.nextLine().split(" ");
            if(s[0].equals("I")) insert(s[1]);
            else wt.println(query(s[1]));
        }
        
        wt.flush();
    }
    
    static class rd
    {
        static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        static StringTokenizer tk=new StringTokenizer("");
        
        static String nextLine() throws IOException
        {
            return bf.readLine();
        }
        
        static String next() throws IOException
        {
            while(!tk.hasMoreTokens()) tk=new StringTokenizer(bf.readLine());
            return tk.nextToken();
        }
        
        static int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        
        static double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
        
        static long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        
        static BigInteger nextBig() throws IOException
        {
            BigInteger d=new BigInteger(rd.nextLine());
            return d;
        }
    }
}

143. 最大异或对 - trie + 二进制

活动 - AcWing

题目:

在给定的N个整数 A1,A2.......AN中选出两个进行xor(异或)运算,得到的结果最大是多少?

思路:

将每个数以二进制方式存入 Trie 树

每一次检索,我们都走与当前 ai 这一位相反的位置走,也就是让异或值最大

如果没有相反位置的路可以走的话,那么就走相同位置的路

/*
    *道阻且长,行则将至*
    author:Roye_ack
*/
import java.util.*;
import java.io.*;
import java.math.*;
 
class Main
{
    static int N=100010,M=31*N;
    static int[] a=new int[N];
    static int[][] son=new int[M][2]; //son[节点1][值]=节点2  节点1的子节点是节点2
    static int idx=0;
    
    static void insert(int x)
    {
        int p=0;
        for(int i=30;i>=0;i--) //最大31位,0~30位
        {
            int u=x>>i&1;
            if(son[p][u]==0) son[p][u]=++idx;
            p=son[p][u];
        }
    }
    
    static int find(int x)
    {
        int p=0,res=0;
        for(int i=30;i>=0;i--)
        {
            int u=x>>i&1;
            if(son[p][u^1]!=0) //如果当前层有对应不同的数,走不同的分支
            {
                p=son[p][u^1];
                res=res*2+1; //该位异或后为1,将其左移i位加到res中
            }
            else 
            {
                p=son[p][u];
                res=res*2+0;
            }
        }
        return res;
    }
    
    public static void main(String[] args) throws IOException
    {
        PrintWriter wt=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        
        int n=rd.nextInt();
        for(int i=0;i<n;i++)
        {
            a[i]=rd.nextInt();
            insert(a[i]);
        }
        int res=0;
        for(int i=0;i<n;i++) res=Math.max(res,find(a[i]));
        wt.print(res);
        wt.flush();
    }
    
    static class rd
    {
        static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        static StringTokenizer tk=new StringTokenizer("");
        
        static String nextLine() throws IOException
        {
            return bf.readLine();
        }
        
        static String next() throws IOException
        {
            while(!tk.hasMoreTokens()) tk=new StringTokenizer(bf.readLine());
            return tk.nextToken();
        }
        
        static int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        
        static double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
        
        static long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        
        static BigInteger nextBig() throws IOException
        {
            BigInteger d=new BigInteger(rd.nextLine());
            return d;
        }
    }
}

 

3485. 最大异或和 - 前缀和+Trie+滑动窗口

3485. 最大异或和 - AcWing题库

题目:

思路:

本题要求异或和的最大值

求某一段区间的异或和可以求前缀异或和,也就是S[r]^S[l-1],因为a^x^x=a

因此题目就转化为求异或前缀和S[1]~S[N]中选一个最大的异或对

维护一个长度不超过m的滑动窗口,把窗口外的数从字典树删除,删除操作只要让cnt--就可以

长度不超过m的连续子数组,我们可以枚举右端点s[i],在合法区间内求最大的另一异或数

最大异或对模板

  • 想要异或结果最大,则每一位都要尽量不同,这样异或结果每一位1的个数才会最多
  • 将每个数字的二进制数(01串),从高位到低位存到trie中
  • 对于每一位ai,都尽量往跟他相反的方向走(比如ai为0则走1分支,1则走0分支),如果实在没有相反的分支,则顺着走
/*
    *道阻且长,行则将至*
    author:Roye_ack
*/
import java.util.*;
import java.io.*;
import java.math.*;
 
class Main
{
    static int N=3100010;
    static int[] s=new int[N],cnt=new int[N]; //cnt记录数的出现次数
    static int[][] tr=new int[N][2]; //son[节点1][值]=节点2  节点1的子节点是节点2
    static int idx=0;
    
    static void insert(int x,int num)
    {
        int p=0;
        for(int i=30;i>=0;i--)
        {
            int u=x>>i&1;
            if(tr[p][u]==0) tr[p][u]=++idx;
            p=tr[p][u];
            cnt[p]+=num;
        }
    }
    
    static int find(int x)
    {
        int p=0,res=0;
        for(int i=30;i>=0;i--)
        {
            int u=x>>i&1;
            if(cnt[tr[p][u^1]]!=0)
            {
                p=tr[p][u^1];
                res=res*2+1;
            }
            else
            {
                p=tr[p][u];
                res*=2;
            }
        }
        return res;
    }
    
    public static void main(String[] args) throws IOException
    {
        PrintWriter wt=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        
        int n=rd.nextInt(),m=rd.nextInt();
        for(int i=1;i<=n;i++)
        {
            int x=rd.nextInt();
            s[i]=s[i-1]^x;
        }
        
        int res=0;
        insert(s[0],1); //刚开始插入s0
        for(int i=1;i<=n;i++) //枚举右端点
        {
            if(i>m) insert(s[i-m-1],-1); //删掉不在区间内的左端点(合法区间是[i-m,i])
            res=Math.max(res,find(s[i]));
            insert(s[i],1);
        }
        wt.print(res);
        wt.flush();
    }
    
    static class rd
    {
        static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        static StringTokenizer tk=new StringTokenizer("");
        
        static String nextLine() throws IOException
        {
            return bf.readLine();
        }
        
        static String next() throws IOException
        {
            while(!tk.hasMoreTokens()) tk=new StringTokenizer(bf.readLine());
            return tk.nextToken();
        }
        
        static int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        
        static double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
        
        static long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        
        static BigInteger nextBig() throws IOException
        {
            BigInteger d=new BigInteger(rd.nextLine());
            return d;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 给定一棵 $n$ 个节点的二叉,每个节点上有一个整数。求二叉中某个子内的最大和最小数的异或和。 输入格式 第一行包含整数 $n$。 以下 $n$ 行每行描述二叉的一个节点,格式为 ID val lson rson,其中 ID 表示节点编号(范围 1∼n),val 是节点上的整数,lson 和 rson 分别表示该节点的左儿子和右儿子的编号。若节点没有左儿子或右儿子,则对应位置为 0。 输出格式 一个整数,表示异或和。 数据范围 1≤n≤10^5,−10^9≤val≤10^9 输入样例1: 5 1 1 2 3 2 2 4 5 3 3 0 0 4 4 0 0 5 5 0 0 输出样例1: 7 输入样例2: 9 1 1 2 3 2 2 4 5 3 3 6 7 4 4 8 9 5 5 0 0 6 6 0 0 7 7 0 0 8 8 0 0 9 9 0 0 输出样例2: 8 算法 (Trie ,后缀数组,分块) $O(n log n + n \log^2 mod)$ C++ 代码 用Trie实现 ``` #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<stdlib.h> #include<time.h> using namespace std; #define f(a,b,c) for(a=b;a<=c;a++) #define g(a,b,c) for(a=b;a>=c;a--) #define ll long long const ll INF=2e9; const int N=1e5+7,M=1e6+7; inline int read(){ int 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<<1)+(x<<3)+ch-'0';ch=getchar();} return x*f; } int e[M],ne[M],h[N],idx; void add(int a,int b){e[idx]=b;ne[idx]=h[a];h[a]=idx++;} int fa[N],dep[N],sz[N],son[N],wson[N],dfn[N],dnt,ndfn[N],tp[N]; struct Trie{ int son[2],cnt; }tr[N*32]; int rt[N]; int cnt; void update(int u,int k){ int p=rt[u],q=rt[cnt++]; int i,j; rt[u]=q,tr[q]=tr[p]; f(i,30,0){ tr[q].cnt=tr[p].cnt+1; j=(k>>i)&1; if(tr[p].son[j]==0){ tr[q].son[0]=tr[p].son[0],tr[q].son[1]=tr[p].son[1]; tr[q].son[j]=cnt++,tr[tr[q].son[j]]=(Trie){0,0}; } p=tr[p].son[j],q=tr[q].son[j]; } tr[q].cnt=tr[p].cnt+1; } int query(int u,int v,int k){ int p=rt[u],q=rt[v],res=0; int i,j; f(i,30,0){ j=(k>>i)&1; if(tr[tr[q].son[j^1]].cnt>tr[tr[p].son[j^1]].cnt) res=res|(1<<i),q=tr[q].son[j^1],p=tr[p].son[j^1]; else q=tr[q].son[j],p=tr[p].son[j]; } return res; } void dfs1(int u,int la){ dep[u]=dep[la]+1,fa[u]=la,sz[u]=1; int i,v,maxn=-1; for(i=h[u];~i;i=ne[i]){ v=e[i]; if(v==la) continue; dfs1(v,u); sz[u]+=sz[v]; if(sz[v]>maxn) maxn=sz[v],son[u]=v; } } void dfs2(int u){ int i,v; dfn[u]=++dnt,ndfn[dnt]=u; if(son[u]) wson[son[u]]=dfn[son[u]],tp[son[u]]=tp[u],dfs2(son[u]); else return; for(i=h[u];~i;i=ne[i]){ v=e[i]; if(v==fa[u]||v==son[u]) continue; wson[v]=dfn[v],tp[v]=v,dfs2(v); } } int find(int u,int v){ int f1=tp[u],f2=tp[v]; int ans=0; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(u,v),swap(f1,f2); ans=max(ans,query(wson[f1],wson[u],v)); u=fa[f1],f1=tp[u]; } if(u==v) return ans; if(dep[u]<dep[v]) swap(u,v); return max(ans,query(wson[son[v]],wson[u],v)); } int main(){ int n=read(); memset(h,-1,sizeof h); int i,a,b,c,ans1=0x3f3f3f3f,ans2=-0x3f3f3f3f; f(i,1,n){ a=read(),b=read(),c=read(); if(c){ add(a,c),add(c,a); add(b,c),add(c,b); } else{ add(a,b),add(b,a); } } dfs1(1,0),wson[1]=dfn[1],tp[1]=1,dfs2(1); rt[0]=cnt++,tr[rt[0]]=(Trie){0,0}; f(i,1,n){ a=ndfn[i]; rt[a]=rt[fa[a]]; update(a,read()); } f(i,1,n){ a=ndfn[i]; ans1=min(ans1,query(rt[1],rt[a],read())); ans2=max(ans2,query(rt[1],rt[a],read())); } cout<<ans1+ans2; return 0; } ``` '''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值