这题我做了三次,三次都把异或看成了与,真心无语
一道基础字典树,记住初始化根
#include <cstdio>
#include <algorithm>
using namespace std;
struct Trie
{
Trie *next[2];
int cnt;
};
Trie *root = new Trie;
void init(Trie *p)
{
p->next[0] = p->next[1] = NULL;
p->cnt = 1;
}
void insert(char *p, int add)
{
Trie *now = root, *newnode;
while(*p != '\0' && now ->next[*p - '0'] != NULL)
{
now = now->next[*p++ - '0'];
now->cnt += add;
}
while(*p != '\0')
{
newnode = new Trie;
init(newnode);
now->next[*p++ - '0'] = newnode;
now = newnode;
}
}
int query(char *p)
{
Trie *now = root;
int res = 0;
while(*p != '\0')
{
res <<= 1;
int t = (*p++ - '0') ^ 1;
if(now->next[t] != NULL && now->next[t]->cnt != 0)
now = now->next[t], res++;
else
now = now->next[t^1];
}
return res;
}
int a[1005];
char s[35];
void toString(int t)
{
for(int i = 31; i >= 0; i--)
s[i] = '0' + (t & 1), t >>= 1;
s[32] = '\0';
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init(root);
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
toString(a[i]);
insert(s, 1);
}
int Max = 0;
for(int i = 0; i < n; i++)
{
toString(a[i]);
insert(s, -1);
for(int j = i + 1; j < n; j++)
{
toString(a[j]);
insert(s, -1);
toString(a[i] + a[j]);
Max = max(Max, query(s));
toString(a[j]);
insert(s, 1);
}
toString(a[i]);
insert(s, 1);
}
printf("%d\n", Max);
}
return 0;
}