Immediate Decodability(链接)
Sample Input
01 10 0010 0000 9 01 10 010 0000 9
Sample Output
Set 1 is immediately decodable Set 2 is not immediately decodable
//模板题
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2;
struct node
{
node *next[maxn];
int num;
}root;
node *newnode()
{
node *p = new node;
p->num = 0;
p->next[0] = NULL;
p->next[1] = NULL;
return p;
}
int buildTree(node *root,char *t)
{
int len = strlen(t);
node *p = root;
for(int i=0;i<len;i++)
{
int a=t[i]-'0';
if(p->num!=0)
return 0;
if(p->next[a]==NULL)
p->next[a]=newnode();
p=p->next[a];
}
if(p->next[0]==NULL&&p->next[1]==NULL&&p->num==0)
{
p->num = 1;
return 1;
}
else
return 0;
}
void freeTree(node *p)
{
for(int i=0;i<2;i++)
{
if(p->next[i])
freeTree(p->next[i]);
}
free(p);
}
char str[1010][1010];
int main()
{
int Case = 1,n=0,i,m;
node *head;
while(~scanf("%s",&str[n++]))
{
if(str[n-1][0]=='9')
{
head=newnode();
for(i=0;i<n-1;i++)
{
m = buildTree(head,str[i]);
if(m==0)
break;
}
if(i==n-1)
printf("Set %d is immediately decodable\n",Case++);
else
printf("Set %d is not immediately decodable\n",Case++);
n=0;
freeTree(head);
}
}
return 0;
}