IMMEDIATE DECODABILITY
Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB
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)
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
Sample Output
Source ZJGSU warmup 1 ————————————————————心酸的分割线———————————————————— 思路:最水的字典树,从字母树变成了01树。一旦插入的时候发现插入的字母(暂且这么说)构成了单词,那么not。 代码如下:
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)
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
Original | Transformed |
01 10 0010 0000 9 01 10 010 0000 9
Sample Output
Original | Transformed |
Set 1 is immediately decodable Set 2 is not immediately decodable
Source ZJGSU warmup 1 ————————————————————心酸的分割线———————————————————— 思路:最水的字典树,从字母树变成了01树。一旦插入的时候发现插入的字母(暂且这么说)构成了单词,那么not。 代码如下:
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
char dic[10][20];
struct TrieNode {
TrieNode* next[2];
bool word;
}node[200];
int nxt, k = 1, flag;
int cmp(const void* a, const void* b) {
return strcmp((char*)a, (char*)b);
}
TrieNode *newnode() {
memset(&node[nxt], 0, sizeof(TrieNode));
return &node[nxt++];
}
void Insert(TrieNode* rt, char* s) {
int len = strlen(s);
for(int i = 0; i < len; i++) {
int c = s[i] - '0';
if(!rt->next[c]) rt->next[c] = newnode();
rt = rt->next[c];
if(rt->word) {
flag = 0;
break;
}//if写在了rt = rt->next的后面,因为此时rt才指向刚插入的字母
}
rt->word = true;
}
int main() {
//freopen("test.in", "r", stdin);
while(~scanf("%s", dic[0])) {//其实是处理到文件结束
int cas;
nxt = 0; flag = 1;
TrieNode* root = newnode();
for(cas = 1;;cas++) {
scanf("%s", dic[cas]);
if(dic[cas][0] == '9') break;
}//插入到输入9为止
qsort(dic, cas, sizeof(dic[0]), cmp);//这里,必须注意。不进行排序的话,先输入101 后输入10,答案是错的
for(int i = 0; i < cas; i++) {
Insert(root, dic[i]);
}
if(flag)
printf("Set %d is immediately decodable\n", k++);
else
printf("Set %d is not immediately decodable\n", k++);
}
return 0;
}
更多推荐
AOJ-AHU-OJ-13(POJ-1056) IMMEDIATE DECODABILITY
发布评论