UVA 213 java

未解决runtime error
有时间再来解决
即坑点
在输入时可能000出现在字段末尾但又不是结束小节,需要额外进行处理。
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	while(sc.hasNext()) {
    		String s=sc.nextLine();
    		HashMap<String, Character> map = new HashMap<String,Character>();
    		int len=1;//串的长度,从1开始最多为7
    		int count=0;//1~2*len-1
    		for (int i = 0; i < s.length(); i++) {
    			String s1=Integer.toBinaryString(count);
    			String zero="";
    			for (int j = 0; j < len-s1.length(); j++) {
    				zero+="0";
    			}
    			s1=zero+s1;
    			map.put(s1, s.charAt(i));
				count++;
				//当count=2*len-1,长度为len的已经转换完毕,长度len+1;count又从0开始
				if(count==(Math.pow(2, len)-1)) {
					len++;
					count=0;
				}
			}
    		String s2="";//解码串
    		while(true) {
    			String temp=sc.nextLine();
    			s2+=temp;
    			if(temp.substring(temp.length()-3, temp.length()).equals("000")) {
    				break;
    			}
    		}
    		int code_len_start=0;
    		while(true) {
    			if(s2.substring(code_len_start,code_len_start+3).equals("000")) {
    				System.out.println();
    				break;
    			}
    			int code_len=Integer.parseInt(s2.substring(code_len_start,code_len_start+3),2);
    			String code_end=Integer.toBinaryString((1<<code_len)-1);
    			for (int i = code_len_start+3; ; i+=code_len) {
					String cur=s2.substring(i,i+code_len);
					if(cur.equals(code_end)) {
						code_len_start=i+code_len;
						break;
					}
					System.out.print(map.get(cur));
				}
    		}
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值