【数论-异或】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
#include
#include
#include
#include
#include
#include
#include
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);
    }
}


你可能感兴趣的:(【数论-异或】nyoj-备用 2345: A problem that's so easy)