#include<bits/stdc++.h> usingnamespace std; map<string, bool> m1; map<string, int> m2; char s[2000005]; int cnt = 0; int trie[1005][28]; bool f[2000010]; bool flag[10005]; inlineintread() { int ans = 0, fl = 1; char i = getchar(); while (i < '0' || i > '9') { if (i == '-') { fl = -1; } i = getchar(); } while (i >= '0' && i <= '9') { ans = ans * 10 + i - '0'; i = getchar(); } return ans * fl; } inlinevoidinsert() { scanf("%s", s + 1); int len = strlen(s + 1); int pos = 0; for (int i = 1; i <= len; i++) { int c = s[i] - 'a'; if (!trie[pos][c]) { trie[pos][c] = ++cnt; } pos = trie[pos][c]; } flag[pos] = 1; } inlineintquery()//查询 { scanf("%s", s + 1); if (m1[s + 1]) { return m2[s + 1]; } else { int len = strlen(s + 1); int pos = 0; int ans; memset(f, false, sizeof(f)); f[0] = true; for (int i = 0; i <= len; i++) { if (!f[i]) continue; else ans = i; for (int j = i + 1, p = 0; j <= len; j++) { int c = s[j] - 'a'; p = trie[p][c]; if (!p) break; if (flag[p]) f[j] = true; } } m1[s + 1] = true; m2[s + 1] = ans; return ans; } } intmain() { int n, m; n = read(); m = read(); while (n--) insert(); while (m--) cout << query() << endl; return0; }
inline ll read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return (ll)x * f; } stack<int>stk; int ttre[3100006][2],ed[3100006],k[100005];//val[300005]; int c; int ans=0;
intmain() { int n,g; cin >> n; c=1; for(int j=1;j<=n;j++) { scanf("%d", &k[j]); int p = 0; for (int i = 30; i>=0; i--) { g = (k[j]>>i)&1; //printf("%d*",g); if (!ttre[p][g]) ttre[p][g] = c++; p = ttre[p][g]; //val[p]=g; } ed[p]++; } int ans=0; for(int i=1;i<=n;i++) { int tmp=0,p=0; for(int j=30;j>=0;j--) { if(ttre[p][!((k[i]>>j)&1)]) { tmp+=1<<j; p=ttre[p][!((k[i]>>j)&1)]; } else p=ttre[p][((k[i]>>j)&1)]; } ans=max(ans,tmp); } cout<<ans; return0; }
E Phone List
题目大意
给定 n 个长度不超过 10 且各异的数字串,问其中是否存在两个数字串 S,T,使得 S 是 T 的前缀。
inline ll read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return (ll)x * f; } int g; int ttre[100005][10], meet[100005], ed[100005], c; char k[12]; intmain() { int n; itn t; cin >> t; while (t--) { int f = 0; cin >> n; c = 1; while (n--) { scanf("%s",&k); int p = 0; for (int i = 0; k[i]; i++) { g = k[i] - '0'; //printf("%d*",g); if (!ttre[p][g]) ttre[p][g] = c++; p = ttre[p][g]; meet[p]++; } //meet[p]--; ed[p]++; } for (int i = 0; i < c; i++) { if (ed[i] && meet[i] >= 2) f = 1; ed[i]=0; meet[i]=0; memset(ttre[i],0,sizeof ttre[i]); } if (f) printf("NO\n"); else printf("YES\n"); } return0; }
inline ll read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return (ll)x * f; } constint N = 1e5 + 10; int trie[N * 31][2], mx[N * 31], fa[N * 31], tot = 0;
voidinit() { trie[0][0] = trie[0][1] = 0; for (int i = 0; i <= tot; i++) { mx[i] = fa[i] = 0; } tot = 0; }
voidinsert(int x, int pos) { int temp[31]; for (int i = 30; i >= 1; i--) { temp[i] = x & 1; x >>= 1; } int now = 0; for (int i = 1; i <= 30; i++) { int p = now; mx[now] = pos; if (trie[now][temp[i]]) now = trie[now][temp[i]]; else { trie[now][temp[i]] = ++tot; trie[tot][0] = trie[tot][1] = 0; now = tot; } fa[now] = p; } mx[now] = pos; }
intquery(int r, int k, int now, int sum, int bit) { if (sum >= k) return mx[now]; if (sum + (1 << (bit + 1)) <= k) return0; int ans = 0; if (trie[now][0]) { int temp = (r & (1 << bit)); ans = max(ans, query(r, k, trie[now][0], sum + temp, bit - 1)); } if (trie[now][1]) { int temp = (r & (1 << bit) ^ (1 << bit)); ans = max(ans, query(r, k, trie[now][1], sum + temp, bit - 1)); } return ans; }
intmain() { int T = 1; T = read(); while (T--) { init(); int n, k; n = read(); k = read(); int ans = n + 1, l = -1, r = -1; int sum = 0; for (int i = 1; i <= n; i++) { int a = read(); sum ^= a; if (sum >= k) { if (ans > i) { ans = i; l = 1; r = i; } }
int d = query(sum, k, 0, 0, 29); if (d > 0 && ans > i - d) { ans = i - d; l = d + 1; r = i; }
insert(sum, i); } if (ans == n + 1) printf("-1\n"); else printf("%d %d\n", l, r); } }