1、對a[i] 異或(0/1)建立trie陣列。
2、 從trie數從根開始遍歷,找與a[i]相反的數
3、找到當前不一樣的分支,並往前走。
1、建trie 2、與輸入二進位制,選擇反方向異或,得到最大值 3、最後返回最大值7。
在給定的n個整數a1,a2……ana1,a2……an中選出兩個進行xor(異或)運算,得到的結果最大是多少?
輸入格式
第一行輸入乙個整數n。
第二行輸入n個整數a1a1~anan。
輸出格式
輸出乙個整數表示答案。
資料範圍
1≤n≤1051≤n≤105,
0≤ai<2310≤ai<231
輸入樣例:
3
1 2 3
輸出樣例:
3
#include #include using namespace std;
const int n = 100010, m = 3000000;//m表示節點個數
//trie
int n;
int son[m][2], idx;
int a[n];
//x >> i & 1 :判斷x的第i位到底是0還是1
//31位長度二進位制數
void insert(int x)
}int query(int x)
else p = son[p][s];//如果遍歷第i個位置沒有這個數,則只能走反向的位置
}return res;
}int main()
int res = 0;
for (int i = 0; i < n; i ++) res = max(res, query(a[i]));
cout << res << endl;
return 0;
}
最大異或對
acwing 143.最大異或對 在給定的n個整數a1,a2 an中選出兩個進行xor 異或 運算,得到的結果最大是多少?輸入格式第一行輸入乙個整數n。第二行輸入n個整數a1 an。輸出格式輸出乙個整數表示答案。資料範圍1 n 105,0 ai 231 輸入樣例 3 1 2 3 輸出樣例 題解 異或...
最大異或對
最大異或對 題解 這題採用的是01字典樹,一般這種異或問題都採用的是01字典樹樹解決。includeusing namespace std const int n 2e6 1 typedef long long ll ll a n int cnt 1 int tree 3 n 2 void inse...
最大異或對
在給定的n個整數a1,a2 an中選出兩個進行xor 異或 運算,得到的結果最大是多少?輸入格式 第一行輸入乙個整數n。第二行輸入n個整數a1 an。輸出格式 輸出乙個整數表示答案。資料範圍 1 n 105,0 ai 231 輸入樣例 31 2 3 include using namespace s...