inlineintrd(){ bool f = 0; int x = 0; char c = getchar(); for (;!isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? -x : x; }
structnode { int nxt[26], cnt; }c[500007];
int nodecnt; string s;
voidinsert(){ int rt = 0, len = s.length(); for (int i = 0; i < len; ++i) { int w = s[i] - 'a'; if (!c[rt].nxt[w]) c[rt].nxt[w] = ++nodecnt; rt = c[rt].nxt[w]; } }
intquery(){ int rt = 0, len = s.length(); for (int i = 0; i < len; ++i) { int w = s[i] - 'a'; if (!c[rt].nxt[w]) return0; rt = c[rt].nxt[w]; } return ++c[rt].cnt; }
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { cin >> s; insert(); } int m = rd(); for (int i = 1; i <= m; ++i) { cin >> s; int ans = query(); if (ans == 0) puts("WRONG"); elseif (ans == 1) puts("OK"); elseputs("REPEAT"); } return0; }
inlineintrd(){ bool f = 0; int x = 0; char c = getchar(); for (;!isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? -x : x; }
structnode { int nxt[26], cnt, fl; }c[100007];
int nodecnt; string s;
voidinsert(){ int rt = 0, len = s.length(); for (int i = 0; i < len; ++i) { int w = s[i] - '0'; if (!c[rt].nxt[w]) c[rt].nxt[w] = ++nodecnt; rt = c[rt].nxt[w]; ++c[rt].cnt; } c[rt].fl = 1; }
inlinevoidwork(){ for (int i = 0; i <= nodecnt; ++i) { c[i].cnt = c[i].fl = 0; memset(c[i].nxt, 0, sizeof(c[i].nxt)); } nodecnt = 0; int n = rd(); for (int i = 1; i <= n; ++i) { cin >> s; insert(); } for (int i = 1; i <= nodecnt; ++i) if (c[i].fl && c[i].cnt >= 2) {puts("NO"); return;} puts("YES"); }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
#include<bits/stdc++.h> #define ll long long #define N 1007 #define mod 998244353 usingnamespacestd;
inlineintrd(){ bool f = 0; int x = 0; char c = getchar(); for (;!isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int a[1 << 17], l, r, totnode;
structnode { int nxt[2]; }c[2000007];
voidbuild(int x){ int rt = 0; for (int i = 17; i >= 0; --i) { int w = ((x & (1 << i)) > 0); if (c[rt].nxt[w] == 0) c[rt].nxt[w] = ++totnode; rt = c[rt].nxt[w]; } }
intcheckmx(int x){ int rt = 0, res = 0; for (int i = 17; i >= 0; --i) { int w = ((x & (1 << i)) > 0); if (c[rt].nxt[w ^ 1]) { res += (1 << i); rt = c[rt].nxt[w ^ 1]; } else rt = c[rt].nxt[w]; } return res; }
intcheckmn(int x){ int rt = 0, res = 0; for (int i = 17; i >= 0; --i) { int w = ((x & (1 << i)) > 0); if (c[rt].nxt[w]) { rt = c[rt].nxt[w]; } else { res += (1 << i); rt = c[rt].nxt[w ^ 1]; } } return res; }
inlinevoidwork(){ for (int i = 0; i <= totnode; ++i) for (int j = 0; j < 2; ++j) c[i].nxt[j] = 0; totnode = 0; l = rd(); r = rd(); for (int i = 1; i <= r - l + 1; ++i) build(a[i] = rd()); for (int i = 1; i <= r - l + 1; ++i) { if (checkmx(a[i] ^ l) == r && checkmn(a[i] ^ l) == l) {printf("%d\n", a[i] ^ l); return;} } }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
Post title:Trie
Post author:Eva.Q
Create time:2022-04-23 15:22:28
Post link:https://qyy/2022/04/23/Algorithm/Trie/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.