您的位置:首页 > 编程语言 > Java开发

算法竞赛入门经典java版程序ch4 UVa213

2017-08-19 11:00 330 查看
本题runtimeError,还没找到原因。

以后有时间再来找问题。

package ch4.uva213;

import java.util.HashMap;
import java.util.Scanner;

//UVa213
//此题runtime Error。 刘汝佳的题解可以过。但不如是何原因
class Main {

static final int x = 8;
static String[] dic = new String[8*(1<<8)];// 题目中的2进制表
static String[] dic2 = new String[10];// 不大于7的数的2进制表
static String header = "";
static String encode = "";

static HashMap<String, String> map = new HashMap<String, String>();

// 整数转2进制字符串,补前导零
public static String int2b(int a, int cnt) {
// System.out.println("a= "+a+" cnt="+cnt);
String leaderZero = "";
String b = Integer.toBinaryString(a);
for (int i = 0; i < cnt - b.length(); i++) {
leaderZero += "0";
}
b = leaderZero + b;
return b;
}

// 2进制字符串,查表得到整数
public static int b2Int(String s) {
for (int i = 0; i < 10; i++) {
if (dic2[i].equals(s)) {
return i;
}
}
return 0;
}

// 按题打表dic,dic2
public static void init() {
int count = 0;
String[][] temp = new String[x][];
for (int i = 0; i < x; i++) {
int cnt = (int) Math.pow(2, i) - 1;// 有几个数
temp[i] = new String[cnt];
for (int j = 0; j < cnt; j++) {
int k = count++;
dic[k] = int2b(j, i);// 位数为i
//System.out.print(dic[k]+",");
}
}
// 初始化 1-7的2进制字串
for (int i = 0; i < 10; i++) {
dic2[i] = int2b(i, 3);
}
}

//全111判断
public static boolean isAll1(String s){
char[] chrArr=s.toCharArray();
for(int i=0;i<chrArr.length;i++){
if(chrArr[i]=='0')
return false;
}
return true;
}

public static void makeMap(String line) {
char[] chrArr = line.toCharArray();
for (int i = 0; i < chrArr.length; i++) {
map.put(dic[i], "" + chrArr[i]);
}
}

public static void print() {
for (int i = 0; i < 256; i++) {
System.out.print(dic[i] + ",");
}
}

public static void main(String[] args) {
init();
// print();
Scanner cin = new Scanner(System.in);
String line = "";
boolean isEncoder = false;
int count=0;
BE:
while ((line = cin.nextLine()) != null) {//文件尾

//header编码头
if(line.length()<1)continue;
if (!(line.charAt(0) == '0' || line.charAt(0) == '1')) {
//初始化所有数据
map.clear();
encode="";
header="";
//
header = line;
makeMap(line);// 将head的每一个字母与 code进行映射
isEncoder = true;
if(count!=0){ //在除第1行外,前面打回车
System.out.println();
}
continue;
}
if (isEncoder == true) {
encode += line;
//System.out.println("encode=" + encode);

if (encode.length()>=3&&encode.substring(encode.length() - 3, encode.length()).equals("000")) {
count++;
// 每次读取len个字符并查表输出
int i = 0;
String key = "";
boolean isStart = true;
int len = 0;
while (true) {
if(i>=encode.length()-3){
break;
}
if (isStart) {
// 算第一段的长度
len = b2Int(encode.substring(i, i+3));
i = i + 3;
if(len==0) continue BE;
// String
// newEncode=encode.substring(len-1,encode.length()-3);
isStart = false;

} else {
key = encode.substring(i, i + len);
//System.out.println("i="+i+" "+key+", ");
i = i + len;
if ( isAll1(key)) {// 全1 结尾
isStart = true;
continue;
}
System.out.print(map.get(key));
}

}
}
}
}
}
}

/*

TNM AEIOU
0010101100011
1010001001110110011
11000
$#**\
0100000101101100011100101000

$#* *\
0100000101101100011100101000

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: