Immediate Decodability(字典树)
Immediate Decodability
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2248 Accepted Submission(s):
1168
点我
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)
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).
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.
#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(字典树)的更多相关文章
- hdu 1305 Immediate Decodability(字典树)
Immediate Decodability Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- (step5.1.2)hdu 1305(Immediate Decodability——字典树)
题目大意:输入一系列的字符串,判断这些字符串中是否存在其中的一个字符串是另外一个字符串的前缀.. 如果是,输出Set .. is not immediately decodable 否则输出Set . ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- hdu1305Immediate Decodability(字典树)
这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. Problem Description An encoding of a set of symbols is said to ...
- HDU1305 Immediate Decodability (字典树
Immediate Decodability An encoding of a set of symbols is said to be immediately decodable if no cod ...
- HDU1305 Immediate Decodability(水题字典树)
巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- 【转】cocos2d-x windows开发环境配置
声明:本教程在参考了以下博文,并经过自己的摸索后实际操作得出,本教程系本人原创,由于升级后的cocos2d-x有了一些变化,目前的博文还没有关于Cocos2d-x 2.2.1最新版搭建Android交 ...
- HTML5 canvas 在线画笔绘图工具(二)
Canvas+Javascript 带图标的工具条制作 TToolbar 工具条是由一个TToolbar对象和两个按钮对象(TImageButton.TColorButton)组成,因为之前我大部分时 ...
- java设计模式之Proxy(代理模式)
java设计模式之Proxy(代理模式) 2008-03-25 20:30 227人阅读 评论(0) 收藏 举报 设计模式javaauthorizationpermissionsstringclass ...
- re模块
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...
- postgresql数据库导入导出
在shell中用命令pg_dump将数据库data1导出到一个文件中 pg_dump -d data1 -f test.txt 或者 pg_dump -d data1 > test.sql 然后 ...
- ansilbe 入门001、ansible的介绍
概述: ansible 作为一个配置管理工具.首先我们要“告诉”它管理的是那几台机器啊:而这个信息就在要ansible 的配置文件中体现了.默认情况下ansible的配置文件保存在 /etc/ansi ...
- Ubuntu14.0.4 64位安装Chrome浏览器
下载链接:translate.google.com.hk/translate?hl=zh-CN&sl=en&u=http://95.31.35.30/chrome/pool/main/ ...
- DOSUSB 2.0 免费版的限制原理
两年前,我在写USB的文章时,多次提到了DOSUSB这个东东,这两年也没有关注这方面的变化,最近,有机会重新进入DOSUSB的官方网站(www.dosusb.net),欣喜地发现,这个网站不仅依然存在 ...
- g++ error: expected ‘)’ before ‘*’ token
原本*号前面的类型是我用typedef自定义的类型的,MyType* const p: 发生这样的错误是,编译器根本不知道MyType是什么东西,这是我在C++多重继承中遇到的.MyType是我在基类 ...
- 使用fdisk进行磁盘管理
http://itercast.com/lecture/17 disk是来自IBM的老牌分区软件,几乎所有Linux系统均默认安装 fdisk是一个MBR分区工具,不可用于GPT分区 只有超级用户(r ...