classTrie { public: structNode { bool is_end; Node* son[26]; Node() { is_end = false; for (int i = 0; i < 26; i++) { son[i] = NULL; } } } * root; /** Initialize your data structure here. */ Trie() { root = newNode(); }
/** Inserts a word into the trie. */ 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->is_end = true; }
/** Returns if the word is in the trie. */ 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->is_end; }
/** Returns if there is any word in the trie that starts with the given * prefix. */ boolstartsWith(string prefix){ auto p = root; for (auto c : prefix) { int u = c - 'a'; if (!p->son[u]) returnfalse; p = p->son[u]; } returntrue; } };
/** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
constint N = 1e5 + 10; // 可以理解为一次性先 new N 个节点,每个节点对应一个标号 // son[i][j] 表示标号为 i 的点的第 j 个儿子节点的标号 // cnt[i] 表示以标号为 i 的点结尾的单词个数 // idx 当前分配到了哪个标号 int son[N][26], cnt[N], idx;
char str[N];
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]; }
// 每个数有 31 位,一共有 N 个数,所以需要开辟 31 * N 个 trie // 树节点,每个节点有两个孩子,存储 1 和 0 constint N = 100010, M = 31 * N;
int n; int a[N]; int son[M][2], idx;
voidinsert(int x){ int p = 0; for (int i = 30; i >= 0; i--) { int u = x >> i & 1; if (!son[p][u]) son[p][u] = ++idx; p = son[p][u]; } } intquery(int x){ int p = 0, res = 0; for (int i = 30; i >= 0; i--) { int u = x >> i & 1; if (son[p][!u]) { res = res * 2 + !u; p = son[p][!u]; } else { res = res * 2 + u; p = son[p][u]; } } return res; }
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int res = 0; for (int i = 0; i < n; i++) { insert(a[i]); int t = query(a[i]); res = max(res, a[i] ^ t); } printf("%d\n", res); return0; }