Immediate Decodability问题Java解答
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 a data file. 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.
The Sample Input describes the examples above.
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
HINT
import java.util.Scanner;
class Trie{
class Node{//字典树节点
Node nxt[]=new Node[];//儿子节点
boolean end=false;//是否是一个二进制代码的最后一个节点
int count=;//此节点上通过的字符(数字)个数
} Node head=null;//字典树的根节点 void clear(){
head=new Node();
}
boolean insert(String str)
{//构造字典树
Node p=head;
for(int i=;i< str.length();i++){
p.count++;
if(p.end) return false;//存在前缀
if(p.nxt[str.charAt(i)-'']==null)
p.nxt[str.charAt(i)-'']=new Node();
p=p.nxt[str.charAt(i)-''];
}
p.count++;
if(p.count>) return false;//存在前缀
p.end=true;
return true;
} public static void main(String[] args)
{
Trie tree=new Trie();
Scanner in=new Scanner(System.in);
int count=;
while(in.hasNext())
{
String s = in.next();
count++;
tree.clear();
int flag=;
while (!s.equals("")) {
if(!tree.insert(s)) flag++;
s = in.next();
}
if (flag!=) {// 如果存在前缀,输出
System.out.println("Set "+count+" is not immediately decodable");
}
else {// 如果不存在前缀,输出
System.out.println("Set " +count+" is immediately decodable");
}
}
}
}
Immediate Decodability问题Java解答的更多相关文章
- java解答:有17个人围成一圈(编号0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止,问此人原来的位置是多少号?
package ttt; import java.util.HashMap; import java.util.Map.Entry; /** * 有17个人围成一圈(编号0~16),从第0号的人开始从 ...
- 大公司面试经典数据结构与算法题C#/Java解答
几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...
- 田忌赛马Java解答
你一定听过田忌赛马的故事吧? 如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉200两 ...
- 几个面试经典算法题Java解答
题目一: public class testClockwiseOutput { //顺时针打印一个矩阵 @Test public void test(){ int[][] num = new int[ ...
- Java位运算经典实例
一 源码.反码.补码 正数的源码.反码.补码相同,例如5: 5的源码:101 5的反码:101 5的补码:101 负数的源码.反码.补 ...
- 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)
看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...
- OJ题目JAVA与C运行效率对比
[JAVA]深深跪了,OJ题目JAVA与C运行效率对比(附带清华北大OJ内存计算的对比) 看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. ----------- ...
- LeetCode 628. 三个数的最大乘积
题目描述 LeetCode 628. 三个数的最大乘积 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积. 示例1 输入: [1,2,3] 输出: 6 示例2 输入: [1,2,3 ...
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
随机推荐
- 【译】浅谈微软OneNote的自动化测试工具
当我们向人们介绍OneNote的自动化时,有一个问题被相当频繁地提到,担忧我们的自动化框架中UI层面测试偏少. 我不喜欢基于UI的自动化.我知道在市场上有许多的自动化系统都是基于UI的自动化(点击按钮 ...
- Crossing River poj1700贪心
题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...
- sql优化-总结
1.尽量缩小数据范围. 2.能一个sql解决的,坚决不用两条sql.利用case when或decode. select month_id, corppkno, sum(exportSum_new) ...
- Gallery平滑移动
看了些网上的方法弄了下平滑移动的效果,虽说最后是实现了,实现后发现也不是我想要的效果,对于我幸苦写过的代码先存放在这上面了 package com.layout; import android.con ...
- The Euler function(欧拉函数)
The Euler function Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 1503171912-ny-一道水题
一道水题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 今天LZQ在玩一种小游戏,可是这游戏数有一点点的大,他一个人玩的累.想多拉一些人进来帮帮他.你能写一个程序帮 ...
- DataTable转换实体类
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data. ...
- 转发年浩大神的spfa算法
http://www.cnblogs.com/superxuezhazha/p/5426624.html #include<iostream> #include<stdio.h> ...
- English - consist of 和 compose of 的区别
comprise,compose,consist,constitute,include 这一组动词都有"组成,包含"的意思. comprise v.包含,包括,由……组成(整体): ...
- vs 2005 在IE下断点不起作用
vs2005 加断点调试,ie下不起作用. 1. 点击[开始]->[运行] 命令:regedit. 2. 定位到HKEY_LOCALMACHINE -> SOFTWARE -> Mi ...