原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1305
字典树裸题,如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
const int Max_N = ;
struct Node{
int cnt;
Node *next[];
inline void set(){
cnt = ;
for (int i = ; i < ; i++) next[i] = NULL;
}
};
struct Trie{
Node stack[Max_N], *root, *tail;
void init(){
tail = &stack[];
root = tail++;
root->set();
}
inline Node *newNode(){
Node *p = tail++;
p->set();
return p;
}
inline void insert(Node *x, char *src){
char *p = src;
while (*p != '\0'){
if (!x->next[*p - '']) x->next[*p - ''] = newNode();
x = x->next[*p - ''];
x->cnt++;
p++;
}
}
inline int query(Node *x, char *src){
char *p = src;
while (*p != '\0'){
if (!x || !x->next[*p - '']) return ;
x = x->next[*p - ''];
p++;
}
return x->cnt;
}
inline void insert(char *src){
insert(root, src);
}
inline int query(char *src){
return query(root, src);
}
}trie;
char ret[][], buf[];
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
trie.init();
int i, n = , k = , flag = ;
while (~scanf("%s", buf)){
if ( != strcmp(buf, "")){
trie.insert(buf);
strcpy(ret[n++], buf);
} else if ( == strcmp(buf, "")){
flag = ;
for (i = ; i < n; i++){
if (trie.query(ret[i]) > ){
printf("Set %d is not immediately decodable\n", k++);
flag = ;
break;
}
}
if (!flag) printf("Set %d is immediately decodable\n", k++);
n = , flag = ;
trie.init();
}
}
return ;
}