Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1513 Accepted Submission(s): 766
Problem 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 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
Source
Recommend
Eddy
本题给定一系列的01序列,要问是否有某个是另一个的前缀。
1.字典树解法
数据较小,可以直接用字典树过掉。字典树结构体中增加是否为字符串末尾的标志位。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[20];
bool tag;
struct node
{
node *next[2];
bool flag;
node()
{
next[0]=next[1]=NULL;
flag=false;
}
};
void insert(node *root,char *s)
{
int i=0,len=strlen(s);
node *p=root;
while(s[i])
{
int id=s[i]-'0';
if(p->next[id])
{
p=p->next[id];
if(p->flag)
tag=false;
}
else
{
p->next[id]=new node;
p=p->next[id];
}
i++;
}
p->flag=true;
}
void del(node *root)
{
if(root==NULL)
return ;
del(root->next[0]);
del(root->next[1]);
delete root;
}
int main()
{
int cas=0;
while(~scanf("%s",str))
{
node *Root=new node;
tag=true;
do
{
insert(Root,str);
scanf("%s",str);
}while(strcmp(str,"9"));
del(Root);
if(tag)printf("Set %d is immediately decodable\n",++cas);
else printf("Set %d is not immediately decodable\n",++cas);
}
return 0;
}
2.二叉树解法
用数组模拟二叉树,0往左走,1往右走,每串结束位置做下标记。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str[20];
bool tag;
int BaniTree[20*10];
void insert(char *s)
{
int i=0,id=1;
while(s[i])
{
if(s[i]=='0')
id=id<<1;
else id=(id<<1)|1;
if(BaniTree[id])tag=false;
i++;
}
BaniTree[id]=1;
}
int main()
{
int cas=0;
while(~scanf("%s",str))
{
memset(BaniTree,0,sizeof(BaniTree));
tag=true;
do
{
insert(str);
scanf("%s",str);
}while(strcmp(str,"9"));
if(tag)printf("Set %d is immediately decodable\n",++cas);
else printf("Set %d is not immediately decodable\n",++cas);
}
return 0;
}