日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 6625 three arrays (Trie+dfs)

發(fā)布時(shí)間:2024/4/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU - 6625 three arrays (Trie+dfs) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

題意

兩個(gè)數(shù)組A、BA、BAB,數(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。