博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1056 IMMEDIATE DECODABILITY
阅读量:5818 次
发布时间:2019-06-18

本文共 3418 字,大约阅读时间需要 11 分钟。

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

0110001000009011001000009

Sample Output

Set 1 is immediately decodableSet 2 is not immediately decodable
1 #include
2 #include
3 #include
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 else77 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 }

 

转载于:https://www.cnblogs.com/PrayG/p/5899676.html

你可能感兴趣的文章
感悟贴2016-05-13
查看>>
参加婚礼
查看>>
Java重写equals方法和hashCode方法
查看>>
Spring ’14 Wave Update: Installing Dynamics CRM on Tablets for Windows 8.1
查看>>
MySQL 备份与恢复
查看>>
TEST
查看>>
PAT A1037
查看>>
(六)Oracle学习笔记—— 约束
查看>>
[Oracle]如何在Oracle中设置Event
查看>>
top.location.href和localtion.href有什么不同
查看>>
Gradle之module间依赖版本同步
查看>>
java springcloud版b2b2c社交电商spring cloud分布式微服务(十五)Springboot整合RabbitMQ...
查看>>
d3 v4实现饼状图,折线标注
查看>>
微软的云策略
查看>>
Valid Parentheses
查看>>
nginx 301跳转到带www域名方法rewrite(转)
查看>>
AIX 配置vncserver
查看>>
windows下Python 3.x图形图像处理库PIL的安装
查看>>
【IL】IL生成exe的方法
查看>>
SettingsNotePad++
查看>>