字典树水题。
#include <cstdio>
#include <cstring>
#include <cstdlib> typedef struct Trie {
bool v;
Trie *next[];
} Trie; Trie *root; bool create(char str[]) {
int i = , id;
bool ret = false;
Trie *p = root, *q; while (str[i]) {
id = str[i] - '';
++i;
if (p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(Trie));
q->v = false;
q->next[] = q->next[] = NULL;
p->next[id] = q;
ret = true;
} else {
if (p->next[id]->v)
return false;
}
p = p->next[id];
}
p->v = true; return ret;
} void del(Trie *t) {
if (t == NULL)
return ;
del(t->next[]);
del(t->next[]);
free(t);
} int main() {
int t = ;
char buf[];
bool f = true;
root = (Trie *)malloc(sizeof(Trie));
root->next[] = root->next[] = NULL;
while (scanf("%s", buf) != EOF) {
if (buf[] == '') {
++t;
if (f)
printf("Set %d is immediately decodable\n", t);
else
printf("Set %d is not immediately decodable\n", t);
f = true;
del(root);
root = (Trie *)malloc(sizeof(Trie));
root->next[] = root->next[] = NULL;
} else {
if (f)
f = create(buf);
}
} return ;
}