C++ #include<bits/stdc++.h> usingnamespace std; constint N = 1e5 + 10; int arr[N][30], cnt[N], idx;
voidinsertNUM(string s1) { int p = 0; for(int i = 0; s1[i];i++) { int u = s1[i] - 'a'; if(!arr[p][u]) arr[p][u] = ++idx; p = arr[p][u]; // arr里面的每一个位置,都是指向它的子节点的 }
cnt[p]++; }
intquery(string s1) { int p = 0; for(int i = 0; s1[i];i++) { int u = s1[i] - 'a'; if(!arr[p][u]) return0; p = arr[p][u]; } return cnt[p]; } intmain() { int n; cin >> n;
for (int i = 0; i < n; i++) { char c1; cin >> c1; if (c1 == 'I') { string s1; cin >> s1; insertNUM(s1); } else { string s1; cin >> s1; int num = query(s1); cout << num << endl; } }
#include<bits/stdc++.h> usingnamespace std; constint N = 1e5 + 10, M = 31 * N; int arr[N];
int n; int son[M][2], idx;
voidinsert(int x) { int p = 0; for (int i = 30; i >= 0; i--) { int u = x >> i & 1; // 取出x 右移 i 位后的当前位 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 n; cin >> n; int res = 0; for (int i = 1; i <= n; i++) cin >> arr[i];