滴滴xor(nlogn)的还有一个O(N)的
思路是xor有性质a ^ b ^b =a; 所以统计前缀和是否出现过 #include<bits/stdc++.h> using namespace std; typedef long long ll; map <int, bool> mp; int n, t, q, ans; int main() { ans = 0; scanf("%d", &n); mp[0] = true; while (n--) { scanf("%d", &t); q ^= t; if (mp.count(q)) { q = 0; mp.clear(); mp[0] = true; ans ++; } else { mp[q] = true; } } printf("%d\n", ans); return 0; }
O(n)
#include<bits/stdc++.h> using namespace std; typedef long long ll; bool mp[100001]; queue <int> que; int n, t, q, ans; int main() { ans = 0; scanf("%d", &n); memset(mp, 0, sizeof mp); mp[0] = true; while (n--) { scanf("%d", &t); q ^= t; if (mp[q]) { q = 0; while (!que.empty()) { mp[que.front()] = false; que.pop(); } ans ++; } else { que.push(q); mp[q] = true; } } printf("%d\n", ans); return 0; }