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. py2exe 生成带图标的单个文件实例

    随便拉了个学习时用的测试程序来做的实例,原程序如下: #Filename:for.py count=0 for i in range(1,100,2): count+=i else: print 't ...

  2. YY语音从4.0版本开始是基于Qt的开发过程,以及碰到的问题

    作者:姚冬链接:http://www.zhihu.com/question/21359230/answer/20127715来源:知乎著作权归作者所有,转载请联系作者获得授权. YY语音从4.0版本开 ...

  3. linux系统文件夹的作用 good

    /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 如:环境变量 /etc/rc.d 启动的配置文件和脚本 /home用户主目录的基点,比如用户user的主目录就是/ho ...

  4. CEF中文教程(google chrome浏览器控件) -- Windows下编译Chromium

    CEF中文教程(google chrome浏览器控件) -- CEF简介 2013-04-10 16:48 42928人阅读 评论(4) 收藏 举报  分类: CEF(2)    目录(?)[+]   ...

  5. mCustomScrollbar的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. Linux系统编程(13)——Shell的基本语法

    按照惯例,Shell变量由全大写字母加下划线组成,有两种类型的Shell变量:环境变量和本地变量. 环境变量: 环境变量可以从父进程传给子进程,因此Shell进程的环境变量可以从当前Shell进程传给 ...

  7. MFC DestroyWindow窗口对象和窗口句柄的销毁

    考虑单窗口情况: 假设自己通过new创建了一个窗口对象pWnd,然后pWnd->Create.则销毁窗口的调用次序: 1. 手工调用pWnd->DestroyWindow(): 2. De ...

  8. bzoj1637 [Usaco2007 Mar]Balanced Lineup

    Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000 ...

  9. C#/vbscript/JS如何加密保护HTML/javascript源代码

    原文地址:http://www.coding123.net/article/20121008/encrypt-javascript-by-charp-vbscript.aspx 本文通过将源代码进行u ...

  10. sqlexpress 不用管理工具 sa

    操作步骤: 开始=>运行=>(快捷键:win+R) cmd, 屎劲敲回车. 出现黑色的DOS窗体后,输入如下几条命令: 1.SQLCMD -S (local)\sqlexpress -E ...