poj 1056 IMMEDIATE DECODABILITY
Description
An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another symbol. We will assume for this problem that all codes are in binary, that no two codes within a set of codes are the same, that each code has at least one bit and no more than ten bits, and that each set has at least two codes and no more than eight.
Examples: Assume an alphabet that has symbols {A, B, C, D}
The following code is immediately decodable:
A:01 B:10 C:0010 D:0000
but this one is not:
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)
Examples: Assume an alphabet that has symbols {A, B, C, D}
The following code is immediately decodable:
A:01 B:10 C:0010 D:0000
but this one is not:
A:01 B:10 C:010 D:0000 (Note that A is a prefix of C)
Input
Write a program that accepts as input a series of groups of
records from standard input. Each record in a group contains a
collection of zeroes and ones representing a binary code for a different
symbol. Each group is followed by a single separator record containing a
single 9; the separator records are not part of the group. Each group
is independent of other groups; the codes in one group are not related
to codes in any other group (that is, each group is to be processed
independently).
Output
For each group, your program should determine whether the codes
in that group are immediately decodable, and should print a single
output line giving the group number and stating whether the group is, or
is not, immediately decodable.
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
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 typedef struct Trie 5 { 6 int num; 7 bool extic; 8 struct Trie *next[2]; 9 }Node,*trie; 10 Node *creat() 11 { 12 Node *node=(Node *)malloc(sizeof(Node)); 13 node->num=0; 14 node->extic=false; 15 memset(node->next,0,sizeof(node->next)); 16 return node; 17 } 18 void insert_(trie root,char *str) 19 { 20 trie node=root; 21 char *p=str; 22 int id; 23 while(*p) 24 { 25 id=*p-'0'; 26 if(node->next[id]==NULL) 27 node->next[id]=creat(); 28 node=node->next[id]; 29 ++p; 30 node->num+=1; 31 } 32 node->extic=true; 33 } 34 int finds(trie root,char *str) 35 { 36 trie node=root; 37 char *p=str; 38 int id; 39 while(*p) 40 { 41 id=*p-'0'; 42 node=node->next[id]; 43 ++p; 44 if(node==NULL) 45 return 0; 46 if(node->extic) 47 return 1; 48 } 49 return 1; 50 } 51 int deal(trie T) 52 { 53 int i; 54 if(T==NULL) 55 return 0; 56 for(int i=0;i<2;i++) 57 { 58 if(T->next[i]!=NULL) 59 deal(T->next[i]); 60 } 61 free(T); 62 return 0; 63 } 64 int main() 65 { 66 char str[1000][300]; 67 int k=0,ans=0,flag=0; 68 trie root=creat(); 69 while(scanf("%s",str[k])!=EOF) 70 { 71 //trie root=creat(); 72 //trie root=creat(); 73 if(strcmp(str[k],"9")==0){ 74 if(flag) 75 printf("Set %d is not immediately decodable\n",++ans); 76 else 77 printf("Set %d is immediately decodable\n",++ans); 78 deal(root); 79 trie root=creat(); 80 k=0,flag=0; 81 } 82 else{ 83 if(finds(root,str[k])==1) 84 flag=1; 85 if(flag) 86 continue; 87 insert_(root,str[k]); 88 } 89 } 90 return 0; 91 }