IMMEDIATE DECODABILITY
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9229 | Accepted: 4375 |
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 <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 8 const int sonnum = 2, base = '0'; 9 struct Trie 10 { 11 int num; bool terminal; 12 Trie *son[sonnum]; 13 }; 14 Trie *NewTrie() 15 { 16 Trie *temp = new Trie; 17 temp->num = 1; temp->terminal = false; 18 for (int i = 0; i < sonnum; ++i) temp->son[i] = NULL; 19 return temp; 20 } 21 bool Insert(Trie *pnt, char *s, int len) 22 { 23 Trie *temp = pnt; 24 bool mrk = true; 25 for (int i = 0; i < len; ++i) 26 { 27 if (temp->son[s[i]-base] == NULL) temp->son[s[i]-base] = NewTrie(); 28 else 29 { 30 temp->son[s[i]-base]->num++; 31 if (temp->son[s[i]-base]->terminal == true) 32 mrk = false; 33 } 34 temp = temp->son[s[i]-base]; 35 } 36 temp->terminal = true; 37 return mrk; 38 } 39 40 int main(void) 41 { 42 Trie *tree; 43 char a[20]; int cnt = 1; 44 #ifndef ONLINE_JUDGE 45 freopen("poj2056.in", "r", stdin); 46 #endif 47 while (~scanf("%s", a)) 48 { 49 tree = NewTrie(); 50 bool flag = true; 51 Insert(tree, a, strlen(a)); 52 while (~scanf("%s", a)) 53 { 54 if (a[0] == '9') break; 55 if (false == Insert(tree, a, strlen(a))) 56 flag = false; 57 } 58 // if (a[0] == '9') continue; 59 if (flag == false) printf("Set %d is not immediately decodable\n", cnt); 60 else printf("Set %d is immediately decodable\n", cnt); 61 cnt++; 62 } 63 64 return 0; 65 }
又练习了一遍字典树,标记即可没有什么技术含量,代码还可以优化,习惯这么写了……虽然有点儿麻烦,至少是对的。