POJ 1056 IMMEDIATE DECODABILITY
IMMEDIATE DECODABILITY
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9630 | Accepted: 4555 |
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
题目大意:给定一段编码,每段编码以“9”结束,判断是否有一个编码是另一个编码的前缀。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; typedef struct node { int n; node *next[2]; node() { for (int i = 0; i < 2; i++) { next[i] = NULL; } n = 1; } }TreeNode; void Insert(char str[], TreeNode *pHead) { TreeNode *p = pHead; int nLen = strlen(str); for (int i = 0; i < nLen; i++) { if (p->next[str[i] - '0'] == NULL) { p->next[str[i] - '0'] = new TreeNode; } else { p->next[str[i] - '0']->n++; } p = p->next[str[i] - '0']; } } int Search(char str[], TreeNode *pHead) { int nLen = strlen(str); TreeNode *p = pHead; bool bfind = false; for (int i = 0; i < nLen; i++) { p = p->next[str[i] - '0']; } return p->n; } void Delete(TreeNode *pHead) { for (int i = 0; i < 2; i++) { if (pHead != NULL) { pHead = pHead->next[i]; Delete(pHead); } } delete pHead; } int main() { char str[10][15]; int nCase = 0; int n = -1; TreeNode *pHead = new TreeNode; int flag = 0; while(scanf("%s", str[++n]) != EOF) { if (str[n][0] == '9') { ++nCase; for (int i = 0; i < n ; i++) { if (Search(str[i], pHead) > 1) { printf("Set %d is not immediately decodable\n", nCase); break; } if (i == n - 1) { printf("Set %d is immediately decodable\n", nCase); } } Delete(pHead); pHead = new TreeNode; n = -1; } else { Insert(str[n], pHead); } } return 0; }