1442. Count Triplets That Can Form Two Arrays of Equal XOR
xor sum i to j = xor sum 0 to j ^ 0 to i - 1
xor sum i to j - 1 = sum 0 to j - 1 ^ 0 to i - 1 = j to k = 0 to k ^ 0 to j - 1
两边的xor sum 0 to j - 1可以消掉
所以找两个从0开始xor相等的就是i 跟k
j是其中任意的数
class Solution {
public:
int countTriplets(vector<int>& arr) {
vector<int> v(arr.size());
int sum = 0;
int ans = 0;
for(int i = 0; i < arr.size(); i++){
v[i] = sum;
sum ^= arr[i];
for(int j = 0; j < i; j++){
if(v[j] == sum){
ans += i - j;
}
}
}
return ans;
}
};