xor

给出n个数字a_1,…a_n,问最多有多少个不重叠的非空区间,使得每个区间内数字的xor都等于0,即得出最大的k,使得存在k个区间(l[i],r[i]),满足1<=l[i]<=r[i]<=n,(1<=i<=k),r[i] < r[i+1],(1<=i<=k),且a[l[i]] xor a[l[i]+1] xor … xor a[r[i]] = 0,(1<=i<=k)
输入描述:第一行一个整数n,第二行n个整数a_1,a_2…a_n,对于30%的数据,n<=20,对于100%的数,n<=100000,a_i<=100000;
输出描述:一个整数表示最多的区间个数

import java.util.Scanner;

public class Kqujian {
    static int maxn = 1000000;
    static int[] a = new int[maxn];
    static int[] b = new int[maxn];
    static int[] c = new int[maxn];
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        while(in.hasNext()){
            int ans = 0;
            int pos = -1;
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
                if(a[i] == 0){
                    ans++;
                    pos = i+1;
                }
                b[a[i]]++;
                if(b[a[i]] != 0 && b[a[i]] % 2 == 0){
                    int temp = c[a[i]];
                    if(temp < pos)
                        continue;
                    int temp2 = 0;
                    for (int j = temp + 1; j < i; j++) {
                        temp2 = temp2 ^ a[j];
                    }
                    if(temp2 == 0){
                        ans++;
                        pos = i+1;
                    }

                }else
                    c[a[i]] = i;
            }
            System.out.println(ans);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值