题意:求一段连续的数字使得它们的异或和最大。

思路:首先利用前缀和求sum[i],这样求某段连续数字异或和最大就是求某两个j和i满足sum[i]^sum[j-1]最大,问题就变成了找两个数的异或最大。这个问题可以利用tire树完成。首先将空串加入树,即sum[0]。然后枚举i,对于sum[i]先取反,再在tire树上贪心的找可以匹配到的字符串,具体地说就是找高位尽量相同的。找到以后更新答案,再将sum[i]插入到trie树上。

注意一个问题就是trie树空间要开的足够大。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#define inf -100000000
#define LL long long
#define maxn 100005
#define B 32
using namespace std;
int n;
void convers(unsigned int val,char *str)
{
    str[B]=;
    ; i>=; --i)
    {
        str[i]=val%+';
        val/=;
    }
}
void reconvers(unsigned int val,char *str)
{
    str[B]=;
    ; i>=; --i)
    {
        str[i]=(val%)^+';
        val/=;
    }
}
struct Tire
{
    ][],end[];
    int sz;
    int newNode()
    {
        memset(ch[sz],,sizeof(ch[sz]));
        end[sz++]=;
        ;
    }
    void clear()
    {
        sz=;
        newNode();
    }
    void insert(char *word,int v)
    {
        ;
        ; word[i]; ++i)
        {
            ';
            if(!ch[now][x])
                ch[now][x]=newNode();
            now=ch[now][x];
        }
        end[now]=v;
    }
    int solve(char *word)
    {
        ;
        ; i<B; ++i)
        {
            ';
            ]&&ch[now][])
                now=ch[now][x];
            ])
                now=ch[now][];
            ])
                now=ch[now][];
        }
        return end[now];
    }
};
unsigned ];
Tire tree;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        ; i<=n; ++i)
        {
            scanf("%u",&sum[i]);
            sum[i]^=sum[i-];
        }
        unsigned ;
        tree.clear();
        ];
        convers(,str);
        tree.insert(str,);
        ; i<=n; ++i)
        {
            ];
            reconvers(sum[i],word);
            int p=tree.solve(word);
            ans=max(ans,sum[i]^sum[p]);
            convers(sum[i],word);
            tree.insert(word,i);
        }
        printf("%u\n",ans);
    }
    ;
}

UVALive 4682 XOR Sum (trie)的更多相关文章

  1. HDU 4825:Xor Sum(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意:给出N个数,M个询问,每个询问给出一个X,问在这N个数中哪个数和X异或后结果最大. 思路:可以用Tr ...

  2. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  3. UVALive - 7639 G - Extreme XOR Sum(思维)

    题目链接 题意 给出一个序列,相邻两两异或,生成一个新序列,再相邻两两异或,直到只剩下一个元素,问最后结果为多少.m个查询,每次都有一个待查询区间. 分析 既然有多组查询,n只是1e4,那么可以考虑预 ...

  4. HDU 4825-Xor Sum(trie)

    题意: 给你一组数,开始询问给一个数  求组中与该数异或值最大的数. 分析:根据异或的特点 要想得到的异或值最大 尽可能的让两个数的每位都相反 先把给定的一组数建树,数的最后一位对应的节点保存这个数的 ...

  5. hdu 4825 Xor Sum (建树) 2014年百度之星程序设计大赛 - 资格赛 1003

    题目 题意:给n个数,m次询问,每次给一个数,求这n个数里与这个数 异或 最大的数. 思路:建一个类似字典数的数,把每一个数用 32位的0或者1 表示,查找从高位向底位找,优先找不同的,如果没有不同的 ...

  6. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  7. hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...

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

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  9. 2014百度之星资格赛—— Xor Sum(01字典树)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

随机推荐

  1. JBPM工作流入门总结

    关于JBPM工作流 1.工作流 工作流是一项分离业务操作和系统流程的技术.工作流由实体(Entity).参与者(Participant).流程定义(Flow Definition).工作流引擎(Eng ...

  2. 淘宝初始化css代码

    ;; } body, button, input, select, textarea { font:12px/1.5tahoma, arial, \5b8b\4f53; } h1, h2, h3, h ...

  3. python 练习 8

    #!/usr/bin/python # -*- coding: utf-8 -*- def ntom(x,size,mod): t=[0]*(size) j=0 while x and j<si ...

  4. (13)odoo翻译

    -------------------更新时间:15:52 2016-09-28 星期三 增加模型名翻译17:26 2016-05-20 星期五17:58 2016-05-17 星期二12:14 20 ...

  5. 关于64位WIN7下正确建立JAVA开发环境(转

    1.下载并安装JDK(地址:http://www.oracle.com/technetwor ... ownload-400750.html     先在“Accept License Agreeme ...

  6. hdu----(5053)the Sum of Cube(签到题,水体)

    the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. Gas Station [LeetCode]

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. Run “mvn clean install” in Eclipse

    1.在Maven项目或者pom.xml上右键-->Run As -->"Maven Build..."或者"Run Configuration-->Ma ...

  9. Android中 在显示ImageView时图片上面和下面都出现一段空白区间的解决办法

    开始的时候是在ScrollView中显示ImageView的时候出现这样的问题,以为是要对ScrollView进行设置的,后来发现单独显示一个ImageView的时候也会出现这样的问题,由此才知道是应 ...

  10. 数据库索引<二> 补充前篇

    你要准备的软件有: 最新版 Rsync for windows 服务端:cwRsync_Server_2.1.5_Installer.zip 客户端:cwRsync_2.1.5_Installer.z ...