HDUOJ 1251 统计难题
动态字典树写法
静态字典树写法
HDUOJ 4825 Xor Sum
动态trie做法 546ms
动态字典树写法
struct Node {
int cnt;
Node *next[26];
Node() {
cnt = 0;
for (int i = 0; i < 26; ++i) next[i] = NULL;
}
};
void add(char *s, Node *root) {
Node *cur = root;
while (*s) {
int id = *s - 'a';
if (!cur->next[id]) cur->next[id] = new Node;
cur = cur->next[id];
cur->cnt++;
++s;
}
}
int query(char *s, Node *root) {
Node *cur = root;
while (*s) {
int id = *s - 'a';
if (!cur->next[id]) return 0;
cur = cur->next[id];
++s;
}
return cur->cnt;
}
char s[15];
int main() {
Node *root = new Node;
while (gets_s(s, 15) && strcmp(s, "")) add(s, root);
while(gets_s(s, 15) && strcmp(s, "")) printf("%d\n", query(s, root));
return 0;
}
HDUOJ 1671 Phone List
静态字典树写法
char s[maxn][12];
int len[maxn], idx[maxn], total;
struct Node {
int end;
Node *next[10];
Node() {
end = 0;
for (int i = 0; i < 10; ++i) next[i] = NULL;
}
}node[maxn];
Node *_new() {
Node *ret = &node[total];
total++;
return ret;
}
bool add(char *s, Node *root) {
Node *cur = root;
while (*s) {
int id = *s - '0';
if (!cur->next[id]) cur->next[id] = _new();
cur = cur->next[id];
if (cur->end) return false;
++s;
}
cur->end = 1;
return true;
}
void init() {
CLEAR(node, 0);
total = 0;
}
bool cmp(int a, int b) { return len[a] < len[b]; }
int main() {
int T;
scanf("%d", &T);
while (T--) {
init();
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%s", s[i]), len[i] = strlen(s[i]), idx[i] = i;
sort(idx, idx + n, cmp);
Node *root = _new();
bool ok = true;
for (int i = 0; i < n; ++i)
if (!add(s[idx[i]], root)) {
ok = false;
break;
}
puts(ok ? "YES" : "NO");
}
return 0;
}
字典树在亦或最值方面的应用
HDUOJ 4825 Xor Sum
动态trie做法 546ms
struct Node {
Node *next[2];
Node() { next[0] = next[1] = NULL; }
};
void add(LL x, Node *root) {
Node *cur = root;
for (int i = 32; i >= 0; --i) {
int id = x >> i & 1;
if (!cur->next[id]) cur->next[id] = new Node;
cur = cur->next[id];
}
}
LL query(LL x, Node *root) {
Node *cur = root;
LL ret = 0;
for (int i = 32; i >= 0; --i) {
int id = (x >> i & 1) ? 0 : 1;
if (!cur->next[id]) id = !id;
cur = cur->next[id];
if(id) ret |= 1 << i;
}
return ret;
}
int main() {
int T, kase = 1;
scanf("%d", &T);
while (T--) {
int n, q;
scanf("%d%d", &n, &q);
LL x;
Node *root = new Node;
while (n--) scanf("%lld", &x), add(x, root);
printf("Case #%d:\n", kase++);
while (q--) scanf("%lld", &x), printf("%lld\n", query(x, root));
}
return 0;
}
静态trie做法 452ms,也没快多少。。也许我写的太渣了吧。。。
struct Node {
Node *next[2];
Node() { next[0] = next[1] = NULL; }
}node[maxn*40];
int total;
void init() { total = 0; CLEAR(node, 0); }
void add(LL x, Node *root) {
Node *cur = root;
for (int i = 32; i >= 0; --i) {
int id = x >> i & 1;
if (!cur->next[id]) cur->next[id] = &node[total++];
cur = cur->next[id];
}
}
LL query(LL x, Node *root) {
Node *cur = root;
LL ret = 0;
for (int i = 32; i >= 0; --i) {
int id = x >> i & 1 ? 0 : 1;
if (!cur->next[id]) id = !id;
cur = cur->next[id];
if (id) ret |= 1 << i;
}
return ret;
}
int main() {
int T, kase = 1;
scanf("%d", &T);
while (T--) {
init();
int n, q;
LL x;
scanf("%d%d", &n, &q);
Node *root = &node[total++];
while (n--) scanf("%lld", &x), add(x, root);
printf("Case #%d:\n", kase++);
while (q--) scanf("%lld", &x), printf("%lld\n", query(x, root));
}
return 0;
}