关于1056:IMMEDIATE DECODABILITY 以及字符串的两个函数 和 qsort函数

来源:互联网 发布:vscode搭建kotlin 编辑:程序博客网 时间:2024/06/05 02:35
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
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)
输入
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).
输出
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.
样例输入
0110001000009011001000009

样例输出

Set 1 is immediately decodableSet 2 is not immediately decodable

也就是判断某个字符串是否为其他的前缀。

这里先用快排将多个字符串排序,然后只需要比较相邻的。

因为,假设字符串x和y相邻,如果x不是y的前缀,那么x也必然不是y之后的字符串的前缀,反证法非常简单。

如果直接暴力比较的话貌似也能过,不过字符串数目一旦多了就悲剧了。。。曾经做过一个电话号码的题目,与之类似

#include <iostream>#include <algorithm>#include <string.h>using namespace std;char str[10][12];int cmp(const void* a, const void* b) {    return strcmp((char*)a, (char*)b);}int main(){    char s[12];    int cnt = 0, num = 0;    while(cin >> s) {        int judge = 0;        if(s[0] != '9')        {            strcpy(str[cnt], s);            cnt++;            continue;        }        if(s[0] == '9')        {            num++;            qsort(str, cnt, sizeof(str[0]), cmp);            for(int i = 0; i < cnt - 1; ++i) {                judge = 0;                int len = strlen(str[i]);                if(strncmp(str[i], str[i + 1], len) == 0) {                    judge = 1;                    break;                }            }            cnt = 0;        }        if(judge == 0) cout << "Set " << num << " is immediately decodable" << endl;        else cout << "Set " << num << " is not immediately decodable" << endl;    }    return 0;}

另外一种方法是建立二叉树,0就向左走,1就向右走。

遇到一个新字符串,放到二叉树里面走。如果发现这个字符串对应路径上的所有点都被标记过,那么结果是not;如果已经走过的某条路径中的所有点都在这个字符串对应的路径上,结果也是not。

这样写起来代码量稍微长一点,明天再写。


关于strcmp的用法:

strcmp(a, b);

如果a > b, 返回1;a=b返回0;a<b返回-1。比较的时候逐位比较,直到先遇到'\0'.

strncmp也就是在参数里面多加了一个要比较的字符个数。


关于qsort的用法详见http://blog.csdn.net/ctthuangcheng/article/details/12970853,就不转载了


0 0
原创粉丝点击