[UVA][查、Trie] 644 - Immediate Decodability@Morris' Blog|PChome Online 人新台
2012-09-15 10:51:54| 人797| 回0 | 上一篇 | 下一篇

  Immediate Decodability 

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 a data file. 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.


The Sample Input describes the examples above.

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

在散中有到, 於必只有唯一的一解,
所以你必方式建造成一棵字典(Trie), 然而於某方式都停留在,
如此一才不生模可的情


#include <stdio.h>
typedef struct {
int l, r, s;
} Node;
Node nd[10000];
int size, flag;
void Trie(char s[]) {
static int i, idx;
for(i = 0, idx = 0; s[i]; i++) {
if(s[i] == '0') {
if(!nd[idx].l)
++size, nd[idx].l = size,
nd[size].l = 0, nd[size].r = 0, nd[size].s = 0;
idx = nd[idx].l;
}
else {
if(!nd[idx].r)
++size, nd[idx].r = size,
nd[size].l = 0, nd[size].r = 0, nd[size].s = 0;
idx = nd[idx].r;
}
if(nd[idx].s) {
flag = 1;
return;
}
}
if(nd[idx].l || nd[idx].r)
flag = 1;
nd[idx].s = 1;
}
int main() {
char s[100];
int cases = 0;
while(gets(s)) {
flag = 0, size = 0;
nd[0].l = 0, nd[0].r = 0;
Trie(s);
while(gets(s)) {
if(s[0] == '9') break;
if(flag) continue;
Trie(s);
}
printf("Set %d is %simmediately decodable\n", ++cases, !flag ? "" : "not ");
}
return 0;
}
 

台: Morris
人(797) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][摸] 397 - Equation Elation
此分上一篇:[UVA][骰子翻比] 253 - Cube painting

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86