【数论-异或】nyoj-备用 2345: A problem that's so easy

2345: A problem that's so easy

时间限制: 1 Sec   内存限制: 128 MB
提交: 96   解决: 24
[ 提交][ 状态][ 讨论版]

题目描述

其他的故事就不多说了,但这一季,故事简单到出乎你的想象。

给你n个数的数组,找出异或值为正整数的所有子区间中长度最长的那个。

输入

第一行只有一个整数n。

第二行n个整数Ai

1≤n≤105

0≤Ai≤105

输出

输出这个子区间的长度。

样例输入

4
1 1 1 1
4
1 0 0 1
4
0 5 3 8

样例输出

3
3
4

提示

来源

Practice-Round#2

题意:

思路:相同的数异或等于0,不同的数异或等于1;正着扫一遍,倒着扫一遍,记录异或值不为0的最长的长度;

代码:

#include<cstdio>
#include<iostream>
#include<math.h>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
const long long  mod=1e9+7;
const int maxn=1e5+10;
typedef long long LL;
int a[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        int l=0;
        LL ans=0;
        int res=0;
        for(int i=1; i<=n; i++)
        {
            ans^=a[i];
            if(ans!=0)
                res=i;
        }
        ans=0;
        for(int i=n;i>=1;i--)
        {
            ans^=a[i];
            if(ans!=0)
            {
                res=max(n-i+1,res);
            }
        }
        printf("%d\n",res);
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值