这题看是否

这题能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. Android Studio 删除 Module

    1.选中Module右击,选择 Open Module Settings,打开Project Structure 窗空.(或者选中Module,按F4打开Project Structure窗口) 2. ...

  2. win10 与linux mint双系统 只能进入mint而无法进入windows的解决方案

    新购买了一块ssd,和以前的hdd硬盘一起装双系统:win10和mint ssd:win10       sdb1  sdb2 sdb3    sda2 hdd:  mint         sda1 ...

  3. pam和sasl

    这几天使用在Postfix搭建一个Webmail的平台,用户认证这一块最终使用了PAM.想整理一下思路,让自己对PAM有个更加清晰的认识. 1.      PAM的简介 PAM全称是:Pluggabl ...

  4. FastDFS client for .net

    fastDfs Code: https://code.google.com/p/fastdfs/ FastDfs_Client_DotNet:https://code.google.com/p/fas ...

  5. [Log]ASP.NET之HttpModule拦截404异常

    Httpmodule代码: public class Error404Module : IHttpModule { public void Init(HttpApplication context) ...

  6. dhroid - ioc高级(接口,对象注入)

    下面到了接口对象的注入了解冻吧,现在才是我们的重点,这才是ioc的核心思想,上面的都是android的辅助1.5 对象依赖问题 我们先来将一下对象对象依赖的重要性,很多同学可能只学了android没学 ...

  7. angular开发控制器之间的通信

    一.指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式(单例模式) 二.基于scope继承的方式: 最简单的让控制器之间进行通信的方法 ...

  8. c++的矩阵乘法加速trick

    最近读RNNLM的源代码,发现其实现矩阵乘法时使用了一个trick,这里描述一下这个trick. 首先是正常版的矩阵乘法(其实是矩阵乘向量) void matrixXvector(float* des ...

  9. nginx socket转发设置

    1.添加依赖模块,如下 --with-stream --with-stream_ssl_module 2.nginx.conf 配置,参考说明:ngx_stream_core_module user ...

  10. PHP----实现压缩HTML

    很多时候,我们在做优化处理的时候,会考虑压缩HTML,去掉HTML里面的空格和换行. 具体实现: public function change(){ echo "change"; ...