字典树经典问题

HDU4852 Xor Sum  字典树

题意

Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大,输出K(共有t组样例,(T < 10),N,M(<1=N,M<=100000))

分析

将每个数插入到线段树后,对于每个s贪心的在字典树上走走即可

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;

int tot;
ll o;
int nt[32*maxn][2];
int cnt[32*maxn];
int a[maxn];
int n;

int newnode()
{
    ++tot;
    memset(nt[tot],0,sizeof(nt[tot]));
    return tot;
}

void Insert(int rt, ll k)
{
    for(int i=32;i>=0;i--)
    {
        if(!nt[rt][(k>>i)&1]){
            int ans=newnode();
            nt[rt][(k>>i)&1]=ans;
            rt=ans;
        }
        else
        {
            rt=nt[rt][(k>>i)&1];
        }
    }
}

ll  query(int rt, ll k)
{
   // int sum=0;
    o=0;
    for(int i=32;i>=0;i--){
        if(((k>>i)&1)==1){
            if(nt[rt][0])
            {
                rt=nt[rt][0];
            }
            else {
                rt=nt[rt][1];
                o+=(1ll<<i);

            }
        }
        else{
            if(nt[rt][1]){
                o+=(1ll<<i);
                rt=nt[rt][1];

            }
            else{
                rt=nt[rt][0];
            }
        }
    }
    return o;
}

int main()
{
   int t;
   scanf("%d", &t);
   int m;
   int pp=1;
   while(t--)
   {
       tot=0;
       memset(nt[0], 0 ,sizeof(nt[0]));
       scanf("%d%d", &n, &m);
       for(int i=0;i<n;i++)
       {
           int x;
           scanf("%d", &x);
           Insert(0, x);
       }
       printf("Case #%d:\n", pp++);
       while(m--)
       {
           int q;
           scanf("%d", &q);
           printf("%lld\n",query(0,q) );
       }
   }
   return 0;
}



来源不明

题意

给n个数,从中取两个数,问xor的最大值(1<=1e5)

分析

将n个数插入到字典树中,贪心的在上面走走即可

原文地址:https://www.cnblogs.com/Superwalker/p/8510666.html

时间: 2024-08-30 12:08:10

字典树经典问题的相关文章

HDU 4825 Xor Sum(经典01字典树+贪心)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1555    Accepted Submission(s): 657 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

hdu 1247:Hat’s Words(字典树,经典题)

Hat's Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7282    Accepted Submission(s): 2639 Problem Description A hat's word is a word in the dictionary that is the concatenation of exactly

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

hdu 1075:What Are You Talking About(字典树,经典题)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12617    Accepted Submission(s): 4031 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdoj 1251 统计难题(经典字典树)

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84414    Accepted Submission(s): 31834 Problem Description Contest time again! How excited it is to see balloons floating ar

白话算法与数据结构之【字典树】

1. 什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高.      Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子

简述字典树

字典是用来查阅某一个字或词的,所谓字典树也就是用于查找某一个数字序列或字符串的.字典树又称Trie树,是一种用树状结构存储字符串的数据结构,经典题目有最长公共前缀.单词统计等. 字典树的存储 字典树的存储原理可见下图.树的根节点什么都不存,接下来每一层的一个节点存储一个字母(或数字),直到将所有的字符串存储完毕. 上图中的字典树存储了ans.alpha.cry.bee.beef五个单词. 由上图可以看出,以小写字母为例,字符串在存储时先按照每个单词的首字母分为若干个节点,首字母相同的会进入同一个

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

剑指Offer——Trie树(字典树)

剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高. Trie的核心思想是空间换时间.利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的. Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子右兄弟的方法建树的话,可能会好点.可见,优