2020牛客寒假算法基础集训营4 D.子段异或

D.子段异或

题目链接-子段异或
在这里插入图片描述
在这里插入图片描述
解题思路
a⊕b,如果a、b两个值相同,异或结果为0,所以有a ⊕ b ⊕ a = b,所以维护前缀异或值然后找到相同的计入答案就可以了,用map存一下每次异或出来的结果,如果结果和之前的有重复的,就加上重复的次数

附上代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=0x3f3f3f3f;
const int N=2e5+5;
const int M=1e9+7;
typedef long long ll;
typedef pair<int,int> PII;
int a[N]; 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int n;
	cin>>n;
	int t=0,sum=0;
	map<int ,int> mp;
	mp[0]=1;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		t=a[i]^t;
		if(mp[t]>=1)
			sum+=mp[t];
		mp[t]++;
	}
	cout<<sum<<endl;
	return 0;
}

发布了78 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104279710