HDU - 6625 three arrays (Trie+dfs)
生活随笔
收集整理的這篇文章主要介紹了
HDU - 6625 three arrays (Trie+dfs)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接
題意
兩個(gè)數(shù)組A、BA、BA、B,數(shù)組的順序隨意,你需要得到一個(gè)數(shù)組CCC,滿足C[i]=A[i]xorB[i]C[i] = A[i] xor B[i]C[i]=A[i]xorB[i]且字典序最小
思路
貪心,建01字典樹(shù)從高位先對(duì)相同的數(shù)字進(jìn)行配對(duì),不存在相同的就找不同的
#include <bits/stdc++.h> const int maxn = 3e6 + 5; using namespace std; struct Trie{int nex[maxn][2], cnt[maxn], end[maxn];int p, root; // root = 0int newnode() {memset(nex[p], 0, sizeof(nex[p]));cnt[p] = end[p] = 0;return p++;}void init() {p = 0;root = newnode();}void add(int x) {int now = root;for (int i = 29; i >= 0; --i) {int t = (x >> i) & 1;if (!nex[now][t]) nex[now][t] = newnode();now = nex[now][t];++cnt[now];}end[now] = 1;} }A, B; vector<int> ans; void dfs(int r1, int r2, int x) {int t = min(A.cnt[r1], B.cnt[r2]);A.cnt[r1] -= t;B.cnt[r2] -= t;if (A.end[r1]) {for (int i = 0; i < t; ++i) ans.push_back(x);return;}int a0 = A.nex[r1][0];int a1 = A.nex[r1][1];int b0 = B.nex[r2][0];int b1 = B.nex[r2][1];if (A.cnt[a0] && B.cnt[b0]) dfs(a0, b0, x << 1);if (A.cnt[a1] && B.cnt[b1]) dfs(a1, b1, x << 1);if (A.cnt[a0] && B.cnt[b1]) dfs(a0, b1, x << 1 | 1);if (A.cnt[a1] && B.cnt[b0]) dfs(a1, b0, x << 1 | 1);return ; } int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int T;scanf("%d", &T);while (T--) {int n, num;scanf("%d", &n);A.init();B.init();for (int i = 1; i <= n; ++i) {scanf("%d", &num);A.add(num);}for (int i = 1; i <= n; ++i) {scanf("%d", &num);B.add(num);}ans.clear();dfs(A.root, B.root, 0);sort(ans.begin(), ans.end());for (int i = 0; i < n; ++i) {if (i > 0) printf(" ");printf("%d", ans[i]);}puts("");}return 0; }總結(jié)
以上是生活随笔為你收集整理的HDU - 6625 three arrays (Trie+dfs)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。