//有两个问题,第一个:Trie root 和Trie *root,要选*root.... root只能有第一个输出,然后就结束了...目前还不知道为啥,第二个就是DelTrie完以后要给root动态分配内存...要不然也会和第一个问题一样.......
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
struct Trie{ //字典树定义
Trie* next[2];
int num; //以当前字符串为前缀的单词的数量
Trie() //构造函数
{
int i;
for(i=0; i<2; i++){
next[i] = NULL;
}
num=0;
}
}*root;
void Insert(char word[]) //将字符串s[i]插入到字典树中
{
Trie *p = root;
int i;
for(i=0;word[i];i++){
if(p->next[word[i]-'0']==NULL)
p->next[word[i]-'0'] = new Trie();
p = p->next[word[i]-'0'];
p->num++;
}
}
int Find(char word[]) //返回以字符串s[i]为前缀的单词的数量
{
Trie *p = root;
int i;
for(i=0;word[i];i++){
if(p->next[word[i]-'0']==NULL)
return 0;
p = p->next[word[i]-'0'];
}
return p->num;
}
int DelTrie(Trie* T)
{
int i;
if(T==NULL)
return 0;
for(i=0; i<2; i++)
{
if(T->next[i]!=NULL)
DelTrie(T->next[i]);
}
free(T);
// T = NULL;
// T -> num = 0;
return 0;
}
int main()
{
char s[15][20];
int i=0;
root = new Trie();
int cnt = 1;
while(cin >> s[i]){ //输入单词
if(s[i][0] == '9'){
int j;
for(j=0; j<i; j++){
if(Find(s[j]) > 1){
cout << "Set "<< cnt++ <<" is not immediately decodable" << endl;
break;
}
}
if(j == i)
cout << "Set " << cnt++ << " is immediately decodable" << endl;
DelTrie(root);
root = new Trie();
i = 0;
}
else
Insert(s[i++]);
}
return 0;
}