算法竞赛入门经典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
*/
以后有时间再来找问题。
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
*/
相关文章推荐
- 算法之路二:刘汝佳算法竞赛入门经典 信息解码 UVA213
- 算法竞赛入门经典java版程序ch3 Uva10340
- Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector
- 算法竞赛入门经典程序3-6
- 算法竞赛入门经典 第三章 uVa1225 - Digit Counting
- 算法竞赛入门经典3.2 分子量UVa1586
- [算法竞赛入门经典] UVA 714 - Copying Books
- 算法之路二:刘汝佳算法竞赛入门经典 3.9生成元 UVa1583
- 算法之路二:刘汝佳算法竞赛入门经典 3.7回文词 UVa401
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
- UVA 算法竞赛 入门经典 Volume 2. Data Structures :: Lists 题解合集
- 算法竞赛入门经典3.5 谜题UVa455
- [算法竞赛入门经典] UVA 1471 - Defense Lines
- <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数
- [算法竞赛入门经典] UVA 1152 - 4 Values whose Sum is 0 | POJ 2785
- 算法竞赛入门经典 回文词 UVa401 msg数组
- 算法之路二:刘汝佳算法竞赛入门经典 3.5Tex中的引号 UVa272
- 算法竞赛入门经典-前四章-UVa10071 - Back to High School Physics-重温高中物理-难度0
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
- AOAPC-I: 算法竞赛入门经典 UVa 习题集分类