Immediate Decodability

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2248    Accepted Submission(s):
1168

点我

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 <cstdio>
#include <cstring>
using namespace std;
#define max 2
typedef struct TrieNode
{
int ncount;
struct TrieNode *next[max];
}TrieNode;
TrieNode* createTrieNode()
{
TrieNode* temp=new TrieNode;
temp->ncount=;
for(int i=;i<max;i++)
temp->next[i]=NULL;
return temp;
}
void insertTrie(TrieNode* proot,char* str)
{
TrieNode *temp=proot;
for(int i=;str[i];i++)
{
int t=str[i]-'';
if(temp->next[t]==NULL)
temp->next[t]=createTrieNode();
temp=temp->next[t];
temp->ncount++;
}
}
int searchTrie(TrieNode* p,char *str)
{
for(int i=;str[i];i++)
{
int t=str[i]-'';
p=p->next[t];
if(!p)
return ;
}
return p->ncount;
}
int main()
{
char a[][];
int i=,k=;
TrieNode* root=createTrieNode();
while(gets(a[i]))
{
int count=;
while(a[i][]!='')
{
insertTrie(root,a[i]);
i++;
gets(a[i]);
}
count=i;
for(i=;i<count;i++)
{
if(searchTrie(root,a[i])!=)
{
cout<<"Set "<<k++<<" is not immediately decodable"<<endl;
break;
}
}
if(i==count)
cout<<"Set "<<k++<<" is immediately decodable"<<endl;
i=;
}
}

Immediate 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. hdu1305Immediate Decodability(字典树)

    这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. Problem Description An encoding of a set of symbols is said to ...

  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查找命令find

    -1 linux的查找命令有两个: locate find locate:有一个索引库,故速度快,但是新加入的一般不再索引库中,故可能无法查到 find:搜索速度慢,但是功能及其强大,可以追加命令动作 ...

  2. linux 安装mysqlServer

    先下载mysql安装包 打开 http://dev.mysql.com/downloads/mysql/  选择 linux - Generic 再选择*.tar.gz(最后那两个) 下载完毕后,得到 ...

  3. 添加鼠标右键菜单项(EditPlus为例)

    下载Editplus,发现大多是绿色版,这就导致鼠标右键快捷菜单了,使用起来不方面,上网搜集了下资料,解决方法很简单: 首先进入注册表:regedit 然后如图设置新项. 其中editplus是右键菜 ...

  4. 思维导图MindManager基础教程

    Mindjet MindManager是由美国Mindjet公司开发的一款优秀的思维导图和知识管理软件,能够帮助你将想法和灵感以清晰的树桩形式记录下来,这对梳理工作计划和规划很有帮助. 思维导图(Mi ...

  5. DataTable类

    DataTable是一个使用非常多的类,记得我在刚刚开始学习.Net的时候就已经了解并用过这个类,但如今再来看看,才发现这个类非常之复杂,复杂表现在哪些地方呢?主要是这个类与其他很多类都有关联,也就是 ...

  6. qsort排序算法

      七种qsort排序方法 <本文中排序都是采用的从小到大排序> 一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a ,  ...

  7. 百度地图V1.5 LocalSearch增加浮动窗体的操作内容

     1.初始化LocalSearch控件 LocalSearch = new BMap.LocalSearch(map, { renderOptions : { map : map, panel : & ...

  8. LeeCode-Pow(x, n)

    Implement pow(x, n). double myPow(double x, int n) { ) return 1.0; ) return 1.0/pow(x,-n); ); }

  9. 配置VirtualBox Linux系统与Windows共享文件

    手动方式配置VirtualBox Linux系统与Windows共享文件 首先,要安装VirtualBox自带的扩展工具,这个ISO可以在虚拟主机安装目录下找到. 将这个文件Copy到你的Linux主 ...

  10. CDH 1、CDH简介

    1.Apache Hadoop 不足之处 • 版本管理混乱 • 部署过程繁琐.升级过程复杂 • 兼容性差 • 安全性低 2.Hadoop 发行版 • Apache Hadoop • Cloudera’ ...