别,WX大牛会发飙的。
回复 80楼 UserYuH
我想看看 rofor 写的Trie
拿来当作模板背背也不错的
#include <stdio.h> #include <string.h> #define N 10001 char str[N][201], count[N]; int left[N] = {0}, right[N] = {0}, size[N] = {0}; int root = 0, last = 0; void sbt_rotate(int *t, int *left, int *right) { int k = right[*t]; right[*t] = left[k]; left[k] = *t; size[k] = size[*t]; size[*t] = size[left[*t]] + size[right[*t]]; *t = k; } void sbt_maintain(int *t, int *left, int *right) { if (size[left[left[*t]]] > size[right[*t]]) sbt_rotate(t, right, left); else if (size[right[left[*t]]] > size[right[*t]]) { sbt_rotate(&left[*t], left, right); sbt_rotate(t, right, left); } else return; sbt_maintain(&left[*t], left, right); sbt_maintain(&right[*t], right, left); sbt_maintain(t, left, right); sbt_maintain(t, right, left); } int sbt_search(int t, char *s) { int res; if (t == 0) return 0; if ((res = strcmp(s, str[t])) == 0) return t; return sbt_search(res < 0 ? left[t] : right[t], s); } int sbt_insert(int *t, char *s) { int ret, res; if (*t == 0) { strcpy(str[++last], s); *t = last; count[*t] = 1; return *t; } ++size[*t]; ret = sbt_insert((res = strcmp(s, str[*t]) < 0) ? &left[*t] : &right[*t], s); res ? sbt_maintain(t, left, right) : sbt_maintain(t, right, left); return ret; } int sbt_remove(int *t, char *s) { int cnt = 0, res; if (*t != 0) { if ((res = strcmp(s, str[*t])) == 0) { *t = size[left[*t]] > size[right[*t]] ? left[*t] : right[*t]; ++cnt; } cnt += sbt_remove(&left[*t], s); cnt += sbt_remove(&right[*t], s); } return cnt; } int main(void) { int n, ret; ret = scanf("%d", &n); while (n--) { ret = scanf("%s", *str); if ((ret = sbt_search(root, *str)) != 0) ++count[ret]; else sbt_insert(&root, *str); } for (n = 1; n <= last; ++n) printf("%s %d\n", str[n], count[n]); return 0; }
#include <stdio.h> #include <string.h> #define N 10001 char str[N][201], count[N]; int left[N] = {0}, right[N] = {0}, size[N] = {0}; int root = 0, last = 0; void sbt_rotate(int *t, int *left, int *right) { int k = right[*t]; right[*t] = left[k]; left[k] = *t; size[k] = size[*t]; size[*t] = size[left[*t]] + size[right[*t]]; *t = k; } void sbt_maintain(int *t, int *left, int *right) { int is_left; if ((is_left = size[left[left[*t]]] > size[right[*t]]) || size[right[left[*t]]] > size[right[*t]]) { if (!is_left) sbt_rotate(&left[*t], left, right); sbt_rotate(t, right, left); sbt_maintain(&left[*t], left, right); sbt_maintain(&right[*t], right, left); sbt_maintain(t, left, right); sbt_maintain(t, right, left); } } int sbt_search(int t, char *s) { int res; if (t == 0) return 0; if ((res = strcmp(s, str[t])) == 0) return t; return sbt_search(res < 0 ? left[t] : right[t], s); } int sbt_insert(int *t, char *s) { int ret, res; if (*t == 0) { strcpy(str[++last], s); *t = last; count[*t] = 1; return *t; } ++size[*t]; ret = sbt_insert((res = strcmp(s, str[*t]) < 0) ? &left[*t] : &right[*t], s); res ? sbt_maintain(t, left, right) : sbt_maintain(t, right, left); return ret; } int sbt_remove(int *t, char *s) { int cnt = 0, res = 1; if (*t != 0) { cnt += sbt_remove(&left[*t], s); cnt += sbt_remove(&right[*t], s); if ((res = strcmp(s, str[*t])) == 0) { if (left[*t] == 0 || right[*t] == 0) *t = left[*t] == 0 ? right[*t] : left[*t]; else { int *succ = &right[*t]; while (left[*succ] != 0) succ = &left[*succ]; strcpy(str[*t], str[*succ]); *succ = right[*succ]; } } size[*t] -= cnt; } return cnt + (res == 0); } int main(void) { int n, ret; ret = scanf("%d", &n); while (n--) { ret = scanf("%s", *str); if ((ret = sbt_search(root, *str)) != 0) ++count[ret]; else sbt_insert(&root, *str); } for (n = 1; n <= last; ++n) printf("%s %d\n", str[n], count[n]); return 0; }