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]++; //cnt记录该串出现的次数 }
voidquery(char str[]) { int p = 0; for (int i = 0; str[i]; i++) { int u = str[i] - 'a'; if (!son[p][u]) { cout << 0 <<endl; return; } p = son[p][u]; } cout << cnt[p] <<endl; return; }
intmain() { int m; scanf("%d", &m); while (m--) { char a[2]; scanf("%s%s", a, str); if (a[0] == 'I') { insert(str); } else { query(str); } } return0; }
voidinsert(int x) { int p = 0; for (int i = 30; i >= 0; i--) { int u = x >> i & 1; //常用位运算操作,查询一个二进制数的第k位(右至左) 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]) { p = son[p][!u]; res = res * 2 + !u; } else { p = son[p][u]; res = res * 2 + u; } } return res; }
intmain() { int res = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { insert(a[i]); //只需要边查询边插入,每次遍历只需要查询第i个之前的数字 res = max(res, query(a[i]) ^ a[i]); } cout << res; return0; }