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. php在cli和cgi方式下获取服务器ip的实例

    php cli方式下获取服务器ip php内核没有提供这样的命令,需要借助linux中的命令ifconfig来实现,如下两个函数代码: function getServerIp(){          ...

  2. python 连接Mysql数据库

    1.下载http://dev.mysql.com/downloads/connector/python/ 由于Python安装的是3.4,所以需要下载下面的mysql-connector-python ...

  3. git status简介

    git status命令可以列出当前目录所有还没有被git管理的文件和被git管理且被修改但还未提交(git commit)的文件.. 比如; git status # On branch maste ...

  4. Git学习05 --分支管理02

    1.冲突 产生冲突后,查看readme.txt   可以看到冲突内容 <<<<<<< ======= >>>>>>> ...

  5. IOS开发UI篇-NavigationController的基本使用

    一.简介 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小v ...

  6. js Array数组的使用

    js Array数组的使用   Array是javascript中的一个事先定义好的对象(也可以称作一个类),可以直接使用 创建Array对象 var array=new Array(): 创建指定元 ...

  7. Unattended Setup Software Components (无人值守安装软件组件)

    原文 http://social.technet.microsoft.com/Forums/windows/en-US/d4ad85b4-8342-4401-83ed-45cefa814ec5/una ...

  8. Android 打开URL

    打开链接 Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); it.set ...

  9. http://bassistance.de/jquery-plugins/

    http://bassistance.de/jquery-plugins/ Query插件:手风琴 jQuery插件:自动完成 jQuery插件:留言 jQuery插件:密码验证 jQuery插件:P ...

  10. AngularJS中文介绍

    简介   AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小 ...