voidinsert(int x){ int pos = 0; for (int i = 30; i >= 0; i -- ) { int u = x >> i & 1; if (ne[pos][u] == 0) ne[pos][u] = ++ idx; pos = ne[pos][u]; } }
intquery(int x){ int pos = 0, res = 0; for (int i = 30; i >= 0; i -- ) { int u = x >> i & 1; if (ne[pos][!u] == 0) { pos = ne[pos][u]; } else { res += (1 << i); pos = ne[pos][!u]; } } return res; }
intmain(){ scanf("%d", &n); for (int i = 0; i < n; i ++ ) { scanf("%d", &a[i]); insert(a[i]); }
int res = 0; for (int i = 0; i < n; i ++ ) res = max(res, query(a[i]));
constint N = 1e5+10; bool isEnd[N]; int ne[N][26], idx; classTrie { public: Trie() { memset(isEnd, false,sizeof(isEnd)); memset(ne, 0, sizeof(ne)); idx = 0; } voidinsert(string word){ int pos = 0; for (auto ch : word) { int u = ch - 'a'; if (ne[pos][u] == 0) ne[pos][u] = ++ idx; pos = ne[pos][u]; } isEnd[pos] = true; } boolsearch(string word){ int pos = 0; for (auto ch : word) { int u = ch - 'a'; if (ne[pos][u] == 0) returnfalse; pos = ne[pos][u]; } return isEnd[pos]; } boolstartsWith(string prefix){ int pos = 0; for (auto ch : prefix) { int u = ch - 'a'; if (ne[pos][u] == 0) returnfalse; pos = ne[pos][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); */
classTrie { public: /** Initialize your data structure here. */ bool isEnd; vector<Trie*> children;
Trie() { children = vector<Trie*>(26, NULL); isEnd = false; } /** Inserts a word into the trie. */ voidinsert(string word){ Trie* node = this; for (auto c : word){ if (node->children[c - 'a'] == NULL){ node->children[c - 'a'] = newTrie(); } node = node->children[c - 'a']; } node->isEnd = true; } /** Returns if the word is in the trie. */ boolsearch(string word){ Trie* node = this; for (auto c : word){ node = node->children[c - 'a']; if (node == NULL) returnfalse; } return node->isEnd;
} /** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Trie* node = this; for (auto c : prefix){ node = node->children[c - 'a']; if (node == NULL) returnfalse; } 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); */