Trie算法

题目:最大异或对

题意:给你n个数,让你求出任意两个数的异或值的最大值。
思路:本题如果采用异或操作时间指定会超时,所以就需要优化,我们可以换一种方法去思考这个问题,可以把所有的A[i]的写成二进制,把这些二进制用Trie来存储,在查找第A[i]的异或的最大值的时候,我们可以从头开始找的时候如果有跟A[i]的二进制相反的那么就找相反的,如果没有相反的就找相同的。
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int N  = 3e7+10;
int a[maxn];
int trie[N][2];
int tot = 0;
void Insert(int val){//建树
     int rt = 0;
     for(int i = 30; ~i; i--){
        int x = val>>i&1;//取最高位
        if(!trie[rt][x]){
            trie[rt][x] = ++tot;
        }
        rt = trie[rt][x];
     }
}
int query(int val){
    int rt = 0, res = 0;
    for(int i = 30; ~i; i--){
        int x = val >> i&1;
        if(trie[rt][!x]){//如果有相反的
            res += 1 << i;
            rt = trie[rt][!x];
        }
        else rt = trie[rt][x];//没有相反的
    }
    return res;
}
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        Insert(a[i]);
    }
    int res = 0;
    for(int i = 1; i <= n; i++){
        res = max(res, query(a[i]));
    }
    printf("%d\n", res);

}

题目: 前缀统计

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int trie[maxn][26];
int sum[maxn], tot = 0;
char str[maxn];
void Insert(char *s, int rt)
{
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int x = s[i] - 'a';
        if(!trie[rt][x])
        {
            trie[rt][x] = ++tot;
        }
        rt = trie[rt][x];

    }
    sum[rt]++;
}
int query(char *s, int rt)
{
    int len = strlen(s);
    int ans = 0;
    for(int i = 0; i < len; i++)
    {
        int x = s[i] - 'a';
        if(!trie[rt][x])return ans;
        if(trie[rt][x]) ans += sum[trie[rt][x]];
        rt = trie[rt][x];
    }
    return ans;
}
int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    while(n--)
    {
        scanf("%s", str);
        Insert(str, 0);
    }
    while(m--)
    {
        scanf("%s", str);
        printf("%d\n",query(str, 0));
    }
    return 0;
}

题目:最长异或值路径

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 101000;
const int N  = 1000000;
int a[maxn];
int trie[N][2];
int tot = 0;
int head[N];
struct Node
{
    int v, w, next;
} e[N];
int k = 1;
void add(int u, int v, int w)
{
    e[k].v = v;
    e[k].w = w;
    e[k].next = head[u];
    head[u] = k++;
}
void Insert(int val)
{
    int rt = 0;
    for(int i = 30; ~i; i--)
    {
        int x = val>>i&1;
        if(!trie[rt][x])
        {
            trie[rt][x] = ++tot;
        }
        rt = trie[rt][x];
    }
}
int query(int val)
{
    int rt = 0, res = 0;
    for(int i = 30; ~i; i--)
    {
        int x = val >> i&1;
        if(trie[rt][!x])
        {
            res += 1 << i;
            rt = trie[rt][!x];
        }
        else rt = trie[rt][x];
    }
    return res;
}
void dfs(int u, int fa, int sum)
{
    a[u] = sum;
    for(int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].v;
        if(v != fa)dfs(v, u, sum^e[i].w);
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= n-1; i++)
    {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        add(u, v, w);
        add(v, u, w);
    }
    dfs(0, -1, 0);
    int res = 0;
    for(int i = 0; i < n; i++)Insert(a[i]);
    for(int i = 0; i < n; i++)
    {
        res = max(res, query(a[i]));
    }
    printf("%d\n", res);

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值