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

总时间限制: 
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.
样例输入
01
10
0010
0000
9
01
10
010
0000
9

样例输出

Set 1 is immediately decodable
Set 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用AngularJs编写的简单 益智游戏(附源代码)  这是一个简单的 javascript 项目。这是一个拼图游戏,也包含一个填字游戏。这个游戏玩起来很棒。有两个不同的版本可以玩这个游戏。你也可以玩填字游戏。 关于游戏 这款游戏的玩法很简单。如上所述,它包含拼图和填字游戏。您可以通过移动图像来玩滑动拼图。您还可以选择要在滑动面板中拥有的列数和网格数。 另一个是填字游戏。在这里你只需要找到浏览器左侧提到的那些单词。 要运行此游戏,您需要在系统上安装浏览器。下载并在代码编辑器中打开此项目。然后有一个 index.html 文件可供您修改。在命令提示符中运行该文件,或者您可以直接运行索引文件。使用 Google Chrome 或 FireFox 可获得更好的用户体验。此外,这是一款多人游戏,双方玩家都是人类。 这个游戏包含很多 JavaScript 验证。这个游戏很有趣,如果你能用一点 CSS 修改它,那就更好了。 总的来说,这个项目使用了很多 javascript 和 javascript 库。如果你可以添加一些具有不同颜色选项的级别,那么你一定可以利用其库来提高你的 javascript 技能。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值