这题看是否

这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序。

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)
 
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
 

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

using namespace std;

typedef struct Node

{

struct Node *next[2];

int flag;

}Node,*Tree;

int flag1;

void Creat(Tree &T)

{

T=(Node *)malloc(sizeof(Node));

T->flag=0;

for(int i=0;i<2;i++)

T->next[i]=NULL;

}

void insert(Tree &T,char *s)

{

Tree p=T;

int t;

int l=strlen(s);

for(int i=0;i<l;i++)

{

t=s[i]-'0';

if(p->next[t]==NULL)

Creat(p->next[t]);

p=p->next[t];

if(p->flag>0)

flag1=0;

}

p->flag++;

}

void D(Tree p)

{

for(int i=0;i<2;i++)

{

if(p->next[i]!=NULL)

D(p->next[i]);

}

free(p);

}

int main()

{

char a[30];

int kk=0;

Tree T;

while(scanf("%s%*c",a)!=EOF)

{

Creat(T);

kk++;

flag1=1;

insert(T,a);

while(scanf("%s",a)!=EOF)

{

if(strcmp(a,"9")==0)

break;

insert(T,a);

}

if(flag1)

printf("Set %d is immediately decodable\n",kk);

else printf("Set %d is not immediately decodable\n",kk);

D(T);

}

return 0;

}

hdu1305Immediate Decodability(字典树)的更多相关文章

  1. hdu 1305 Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  2. (step5.1.2)hdu 1305(Immediate Decodability——字典树)

    题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀.. 如果是,输出Set .. is not immediately decodable 否则输出Set . ...

  3. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  4. Immediate Decodability(字典树)

    Immediate Decodability Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. HDU1305 Immediate Decodability (字典树

    Immediate Decodability An encoding of a set of symbols is said to be immediately decodable if no cod ...

  6. HDU1305 Immediate Decodability(水题字典树)

    巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...

  7. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  8. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  9. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

随机推荐

  1. 【linux】nginx options 跨域问题 请求HTTP错误405 用于访问该页的HTTP动作未被许可 Method Not Allowed

    JavaScript JS 跨域问题 HTTP 错误 405 - 用于访问该页的 HTTP 动作未被许可HTTP 错误 405.0 - Method Not Allowed Nginx 处理跨域问题. ...

  2. 【python3】 django2.0 在生成数据库表时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete'

    python: 3.6.4 django: 2.0 models.py 代码如下 # coding: utf-8 from django.db import models from django.co ...

  3. PHP之Composer类库依赖管理神器

    Composer中文版说明见:https://github.com/kaka987/Composer-zh Composer 是PHP的类包依赖管理工具,用它可以轻松的引用第三方类包,类似于node的 ...

  4. delphixe10 android操作 打电话,摄像头,定位等

    XE6 不支持JStringToString.StringTojString.StrToJURI:use Androidapi.Helpers //Splash Image Delphi XE5,XE ...

  5. git分支的相关问题

    场景:github上准备展示vue打包的项目,做预览的功能,首先创建了分支,gh-page,然后成功将dist文件上传到了gh-page分支中,但是,在Settings的时候,发现 并没有gh-pag ...

  6. ConfluenceRemoteUserAuth

    配置confluence使用httpHeader的方式进行登录(目标版本:atlassian-confluence-6.3.3) 前提是已经安装好了Confluence,并且前端使用apache或者n ...

  7. linux系统下kvm虚拟机的安装

    一 KVM虚拟机简介 KVM是kernel-based Virtual Machine的简称,目前已成为学术界的主流VMM之一.KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术) ...

  8. Thinkphp框架下(同服务器下)不同二级域名之间session互通共享设置

    在Thinkphp框架下根目录打开index.php 在头部加入如下代码即可: //入口文件 define('DOMAIN','abc.com');//abc.com换成自己的跟域名 //以下两行是为 ...

  9. html如何让label在div中的垂直方向居中显示?

    设置label的行高 line-height 和div的高度一致即可.

  10. iOS - 引用计数探讨

    <Objective-C 高级编程> 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而 ...