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
题目大意:判断是否存在一串编码是另一串编码的前缀
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. #define max 2
  6. typedef struct TrieNode
  7. {
  8. int ncount;
  9. struct TrieNode *next[max];
  10. }TrieNode;
  11. TrieNode* createTrieNode()
  12. {
  13. TrieNode* temp=new TrieNode;
  14. temp->ncount=;
  15. for(int i=;i<max;i++)
  16. temp->next[i]=NULL;
  17. return temp;
  18. }
  19. void insertTrie(TrieNode* proot,char* str)
  20. {
  21. TrieNode *temp=proot;
  22. for(int i=;str[i];i++)
  23. {
  24. int t=str[i]-'';
  25. if(temp->next[t]==NULL)
  26. temp->next[t]=createTrieNode();
  27. temp=temp->next[t];
  28. temp->ncount++;
  29. }
  30. }
  31. int searchTrie(TrieNode* p,char *str)
  32. {
  33. for(int i=;str[i];i++)
  34. {
  35. int t=str[i]-'';
  36. p=p->next[t];
  37. if(!p)
  38. return ;
  39. }
  40. return p->ncount;
  41. }
  42. int main()
  43. {
  44. char a[][];
  45. int i=,k=;
  46. TrieNode* root=createTrieNode();
  47. while(gets(a[i]))
  48. {
  49. int count=;
  50. while(a[i][]!='')
  51. {
  52. insertTrie(root,a[i]);
  53. i++;
  54. gets(a[i]);
  55. }
  56. count=i;
  57. for(i=;i<count;i++)
  58. {
  59. if(searchTrie(root,a[i])!=)
  60. {
  61. cout<<"Set "<<k++<<" is not immediately decodable"<<endl;
  62. break;
  63. }
  64. }
  65. if(i==count)
  66. cout<<"Set "<<k++<<" is immediately decodable"<<endl;
  67. i=;
  68. }
  69. }

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. PROCEDURE_监测系统_告警信息存储过程—产生告警信息插入告警表

    create or replace procedure proc_alarmlog(in_id   in number, --采集器编码                                 ...

  2. 关于thinkphp中Hook钩子的解析

    在tp框架下的Library/Think 下有一个Hook类,这个是用来以插件形式来实例化类并且执行方法的 static private  $tags       =   array(); 这里有一个 ...

  3. 如何成为一个真正在路上的Linuxer

    Linux 是工具,却更像一个信仰. 写在前面: 本文目的不是教你如何成为一个真正的Linuxer,也没有能力教你成为一个真正的linuxer,而是通过笔者的一些想法试图指引你真正踏上学习linux之 ...

  4. pubwin会员合并

    此博文已移至爬不稳独立博客:www.pubwin2009.net连接:http://www.pubwin2009.net/index.php/post/15.html 我们说下过程(这里,我们要求两个 ...

  5. linux----ulimit 限制

    ulimit -a 显示当前用户的各种限制. ulimit -n 的数值表示每个进程可以打开的文件数目. 一般情况下, ulimit -n 的数值是1024. 当进程打开的文件数目超过此限制时,该进程 ...

  6. WIN7_64位系统安装oracle以及PLSQL方法(不用装32位oracle客户端)

    一.oracle10g安装,比较简单 1.去Oracle网站下载Vista版的Oracle:Oracle Database 10g Release 2 (10.2.0.4) for Microsoft ...

  7. Activiti 5.16用户手册

    From :http://www.mossle.com/docs/activiti/ Table of Contents 1. 简介 协议 下载 源码 必要的软件 JDK 6+ Eclipse Ind ...

  8. Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET

    Ueditor文本编辑器(新浪SAE平台版本) - 下载频道 - CSDN.NET Ueditor文本编辑器(新浪SAE平台版本)

  9. 记事本写hello world_Java

    1 新建记事本,重命名为hello.java 2 打开hello.java,编辑并保存 class Hello { public static void main(String[] args) { S ...

  10. 解决 VM虚拟机网卡无法拉起 的问题

    复制虚拟机后,第二块网卡始终无法正常运作,表现为无法自动获得dhcp地址,且重启后与第一块网卡使用同样的地址,让人苦笑不得,反复重启了很多次都是这样,后面想到ifcfg-eth1的配置文件可能写得太简 ...