Immediate Decodability
2015-07-26 16:10
295 查看
Immediate Decodability
UVA 644
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:
but this one is not:
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).
The Sample Input describes the examples above.
大意:
给一组数据以9结束,判断这组数据中的某一行是否是另外一行的前缀
要点:
接受到的STRING字符串要先排序 才能比较
代码:
#include <iostream>
#include <string>
using namespace std;
string text[10];
string t;
int mycount = 1;
void mysort(int n)
{
for (int i = 0; i < n; i++)
{
string min = text[i];
for (int j = i + 1; j < n; j++)
{
if (text[j] < min)
{
min = text[j];
text[j] = text[i];
text[i] = min;
}
}
}
}
void match(int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
int leng = text[i].length();
t = text[j].substr(0, leng);
if (text[i] == t)
{
cout << "Set " << mycount << " is not immediately decodable" << endl;
mycount++;
return;
}
}
}
cout << "Set " << mycount << " is immediately decodable" << endl;
mycount++;
return;
}
int main()
{
int num = 0;
while (cin >> text[num])
{
if (text[num] == "9")
{
mysort(num);
match(num);
for (int i = 0; i < 10; i++)
text[num] = "";
num = -1;
}
num++;
}
}
Immediate Decodability
UVA 644
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 containinga 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
大意:
给一组数据以9结束,判断这组数据中的某一行是否是另外一行的前缀
要点:
接受到的STRING字符串要先排序 才能比较
代码:
#include <iostream>
#include <string>
using namespace std;
string text[10];
string t;
int mycount = 1;
void mysort(int n)
{
for (int i = 0; i < n; i++)
{
string min = text[i];
for (int j = i + 1; j < n; j++)
{
if (text[j] < min)
{
min = text[j];
text[j] = text[i];
text[i] = min;
}
}
}
}
void match(int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
int leng = text[i].length();
t = text[j].substr(0, leng);
if (text[i] == t)
{
cout << "Set " << mycount << " is not immediately decodable" << endl;
mycount++;
return;
}
}
}
cout << "Set " << mycount << " is immediately decodable" << endl;
mycount++;
return;
}
int main()
{
int num = 0;
while (cin >> text[num])
{
if (text[num] == "9")
{
mysort(num);
match(num);
for (int i = 0; i < 10; i++)
text[num] = "";
num = -1;
}
num++;
}
}
相关文章推荐
- 3.4Shadowpage页表虚拟化
- Path特效之PathMeasure打造万能路径动效
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- c++学习日记 1
- 通过函数回调排序结构体数组
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
- 【linux高级程序设计】(第七章)终端及串口编程 未完成
- 关于分布式事物 转
- 解决问题:Ubuntu 14.04下Eclipse CDT菜单栏失效
- 3.3EPT内存虚拟化
- PHP中COOKIES使用示例
- 3.2 Qemu Memory管理
- Android开发中去掉标题栏显示
- [容器-OpenStack] 一键部署OpenStack
- lynx最新版本下载地址-2015-7
- Essay
- 归并排序和堆排序
- win32下ACE_TAO的编译
- 提高 Linux 上 socket 性能
- 解决VS编译出现Moc'ing文件,系统找不到指定路径问题