// 假设字符串由小写字母构成 int trie[SIZE][26], tot = 1; // 根节点1, 终止点0
// Trie的插入 voidinsert(char* str){ int len = strlen(str), p = 1; for (int k = 0; k < len; k++) { int ch = str[k]-'a'; if (trie[p][ch] == 0) trie[p][ch] = ++tot; p = trie[p][ch]; } end[p] = true; }
// Trie的检索 boolsearch(char* str){ int len = strlen(str), p = 1; for (int k = 0; k < len; k++) { p = trie[p][str[k]-'a']; if (p == 0) returnfalse; } return end[p]; }
int son[MAX_StrLen][26], cnt[MAX_StrLen], idx; // 0 即表示根节点,也是终止点
voidinsert(char *str){ // 插入字符串 int p = 0; for (int i = 0; str[i]; ++i) { int u = str[i] - 'a'; if (!son[p][u]) son[p][u] = ++idx; p = son[p][u]; } ++cnt[p]; }
intquery(char *str){ // 查询字符串出现次数 int p = 0; for (int i = 0; str[i]; ++i ) { int u = str[i] - 'a'; if (!son[p][u]) return0; p = son[p][u]; } return cnt[p]; }
classTrie { public: structNode { bool isEnd = false; int cnt = 0; Node *son[26] = {nullptr}; } *root; Trie() { root = newNode(); } voidinsert(string word){ auto p = root; for (auto c : word) { int u = c - 'a'; if (!p->son[u]) p->son[u] = newNode(); p = p->son[u]; } p->isEnd = true; p->cnt++; } boolsearch(string word){ auto p = root; for (auto c : word) { int u = c - 'a'; if (!p->son[u]) returnfalse; p = p->son[u]; } return p->isEnd; } boolstartsWith(string prefix){ auto p = root; for (auto c : prefix) { int u = c - 'a'; if (!p->son[u]) returnfalse; p = p->son[u]; } returntrue; } };
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> usingnamespace std; constint SIZE=1000010; int trie[SIZE][26], tot = 1; // 初始化,假设字符串由小写字母构成 int ed[SIZE]; int n, m; char str[SIZE];
voidinsert(char* str){ // 插入一个字符串 int len = strlen(str), p = 1; for (int k = 0; k < len; k++) { int ch = str[k]-'a'; if (trie[p][ch] == 0) trie[p][ch] = ++tot; p = trie[p][ch]; } ed[p]++; }
intsearch(char* str){ int len = strlen(str), p = 1; int ans = 0; for (int k = 0; k < len; k++) { p = trie[p][str[k]-'a']; if (p == 0) return ans; ans += ed[p]; } return ans; }
//Author:XuHt #include<cstdio> #include<cstring> #include<iostream> usingnamespace std; constint N = 1000006; int trie[N][27], tot = 1; char s[N];
voidadd(){ int len = strlen(s), p = 1; for (int i = 0; i < len; i++) { int k = s[i] - 'a' + 1; if (!trie[p][k]) trie[p][k] = ++tot; p = trie[p][k]; } ++trie[p][0]; }
voidget(){ int ans = 0, len = strlen(s), p = 1; for (int i = 0; i <= len; i++) { ans += trie[p][0]; p = trie[p][s[i]-'a'+1]; } cout << ans << endl; }
intmain(){ memset(trie, 0, sizeof(trie)); int n, m; cin >> n >> m; while (n--) { scanf("%s", s); add(); } while (m--) { scanf("%s", s); get(); } return0; }
#include<algorithm> #include<cstdio> #include<cstring> #include<iostream> usingnamespace std; constint SIZE = 100010; int trie[SIZE * 32 + 5][2], tot = 1; // 初始化,假设字符串由小写字母构成 int a[SIZE], n, ans;
voidinsert(int val){ // 插入一个二进制数 int p = 1; for (int k = 30; k >= 0; k--) { int ch = val >> k & 1; if (trie[p][ch] == 0) trie[p][ch] = ++tot; p = trie[p][ch]; } }
intsearch(int val){ int p = 1; int ans = 0; for (int k = 30; k >= 0; k--) { int ch = val >> k & 1; if (trie[p][ch ^ 1]) { // 走相反的位 p = trie[p][ch ^ 1]; ans |= 1 << k; } else { // 只能走相同的位 p = trie[p][ch]; } } return ans; }
intmain(){ cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); insert(a[i]); ans = max(ans, search(a[i])); } cout << ans << endl; }
// Author:XuHt #include<algorithm> #include<cstdio> #include<iostream> usingnamespace std; constint N = 100006 * 33; int trie[N][2];
intmain(){ int n; cin >> n; int ans = 0, t = 1; for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); int p = 1; for (int j = 31; j >= 0; j--) { int k = (a >> j) & 1; if (!trie[p][k]) trie[p][k] = ++t; p = trie[p][k]; } p = 1; if (i > 1) { int w = 0; for (int j = 31; j >= 0; j--) { int k = (a >> j) & 1; if (trie[p][k ^ 1]) { w = (w << 1) + (k ^ 1); p = trie[p][k ^ 1]; } else { w = (w << 1) + k; p = trie[p][k]; } } ans = max(ans, w ^ a); } } cout << ans << endl; return0; }
#include<cstdio> #include<cstring> #include<iostream> usingnamespace std; constint u = 100010; int ver[2 * u], edge[2 * u], next[2 * u], head[u], v[u], val[u * 32], a[u * 32][2], f[u]; int n, tot, i, ans, x, y, z;
voidadd(int x, int y, int z){ ver[++tot] = y; edge[tot] = z; next[tot] = head[x]; head[x] = tot; }
voiddfs(int x){ v[x] = 1; for (int i = head[x]; i; i = next[i]) if (!v[ver[i]]) { f[ver[i]] = f[x] ^ edge[i]; dfs(ver[i]); } }
voidins(int x, int y, int temp){ if (y < 0) { val[x] = temp; return; } int z = (temp >> y) & 1; if (!a[x][z]) a[x][z] = ++tot; ins(a[x][z], y - 1, temp); }
intget(int x, int y, int temp){ if (y < 0) return val[x]; int z = (temp >> y) & 1; if (a[x][z ^ 1]) returnget(a[x][z ^ 1], y - 1, temp); else returnget(a[x][z], y - 1, temp); }
intmain(){ while (cin >> n) { memset(head, 0, sizeof(head)); memset(f, 0, sizeof(f)); memset(v, 0, sizeof(v)); tot = 0; for (i = 1; i < n; i++) { scanf("%d%d%d", &x, &y, &z); x++, y++; add(x, y, z); add(y, x, z); } dfs(1); tot = 1; ans = 0; memset(a, 0, sizeof(a)); ins(1, 30, 0); for (i = 1; i <= n; i++) { ans = max(ans, f[i] ^ get(1, 30, f[i])); ins(1, 30, f[i]); } cout << ans << endl; } return0; }
// Author:XuHt #include<algorithm> #include<cstdio> #include<cstring> #include<iostream> #include<vector> usingnamespace std; constint N = 100006; int n, d[N], trie[N * 33][2], tot; vector<pair<int, int> > e[N]; int Head[N], Edge[N * 2], Leng[N * 2], Next[N * 2], num; bool v[N];
voiddfs(int x){ for (int i = Head[x]; i; i = Next[i]) { int y = Edge[i], z = Leng[i]; if (v[y]) continue; v[y] = 1; d[y] = d[x] ^ z; dfs(y); } }
voidadd(int x, int y, int z){ Edge[++tot] = y; Leng[tot] = z; Next[tot] = Head[x]; Head[x] = tot; }
voidThe_xor_longest_Path(){ memset(d, 0, sizeof(d)); memset(trie, 0, sizeof(trie)); memset(v, 0, sizeof(v)); memset(Head, 0, sizeof(Head)); num = 0; v[0] = 1; tot = 1; for (int i = 1; i < n; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); add(u, v, w); add(v, u, w); } dfs(0); int ans = 0; for (int i = 0; i < n; i++) { int p = 1; for (int j = 31; j >= 0; j--) { int k = (d[i] >> j) & 1; if (!trie[p][k]) trie[p][k] = ++tot; p = trie[p][k]; } p = 1; if (i) { int w = 0; for (int j = 31; j >= 0; j--) { int k = (d[i] >> j) & 1; if (trie[p][k ^ 1]) { w = (w << 1) + (k ^ 1); p = trie[p][k ^ 1]; } else { w = (w << 1) + k; p = trie[p][k]; } } ans = max(ans, w ^ d[i]); } } cout << ans << endl; }
intmain(){ while (cin >> n) The_xor_longest_Path(); return0; }