PKU_ACM_1051_ P,MTHBGWB

 P,MTHBGWB

 

原题连接

 

简单的字符匹配的问题,很容易

 

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main
  4. {
  5.     static HashMap<String,String> codeMap = new HashMap<String,String>();
  6.     static HashMap<String,String> ref = new HashMap<String,String>();
  7.     public static void main(String[] args) throws Exception
  8.     {
  9.         initMap();
  10.         readFile();
  11.     }
  12.     static void readFile() throws Exception
  13.     {
  14.         BufferedReader br = new BufferedReader(
  15.             new FileReader("in.in"));
  16.             //new InputStreamReader(System.in));
  17.         int n = Integer.valueOf(br.readLine());
  18.         int count = 0;
  19.         while(count<n)
  20.         {
  21.             process(br.readLine());
  22.             count++;
  23.         }
  24.     }
  25.     static void initMap()
  26.     {
  27.         codeMap.put("A",".-");
  28.         codeMap.put("B","-...");
  29.         codeMap.put("C","-.-.");
  30.         codeMap.put("D","-..");
  31.         codeMap.put("E",".");
  32.         codeMap.put("F","..-.");
  33.         codeMap.put("G","--.");
  34.         codeMap.put("H","....");
  35.         codeMap.put("I","..");
  36.         codeMap.put("J",".---");
  37.         codeMap.put("K","-.-");
  38.         codeMap.put("L",".-..");
  39.         codeMap.put("M","--");
  40.         codeMap.put("N","-.");
  41.         codeMap.put("O","---");
  42.         codeMap.put("P",".--.");
  43.         codeMap.put("Q","--.-");
  44.         codeMap.put("R",".-.");
  45.         codeMap.put("S","...");
  46.         codeMap.put("T","-");
  47.         codeMap.put("U","..-");
  48.         codeMap.put("V","...-");
  49.         codeMap.put("W",".--");
  50.         codeMap.put("X","-..-");
  51.         codeMap.put("Y","-.--");
  52.         codeMap.put("Z","--..");
  53.         codeMap.put("_","..--");
  54.         codeMap.put(".","---.");
  55.         codeMap.put(",",".-.-");
  56.         codeMap.put("?","----");
  57.         ref.put(".-","A");
  58.         ref.put("-...","B");
  59.         ref.put("-.-.","C");
  60.         ref.put("-..","D");
  61.         ref.put(".","E");
  62.         ref.put("..-.","F");
  63.         ref.put("--.","G");
  64.         ref.put("....","H");
  65.         ref.put("..","I");
  66.         ref.put(".---","J");
  67.         ref.put("-.-","K");
  68.         ref.put(".-..","L");
  69.         ref.put("--","M");
  70.         ref.put("-.","N");
  71.         ref.put("---","O");
  72.         ref.put(".--.","P");
  73.         ref.put("--.-","Q");
  74.         ref.put(".-.","R");
  75.         ref.put("...","S");
  76.         ref.put("-","T");
  77.         ref.put("..-","U");
  78.         ref.put("...-","V");
  79.         ref.put(".--","W");
  80.         ref.put("-..-","X");
  81.         ref.put("-.--","Y");
  82.         ref.put("--..","Z");
  83.         ref.put("..--","_");
  84.         ref.put("---.",".");
  85.         ref.put(".-.-",",");
  86.         ref.put("----","?");
  87.     }
  88.     static void process(String str)
  89.     {
  90.         StringBuffer strCode = new StringBuffer(str);
  91.         StringBuffer lengthCode = new StringBuffer();
  92.         StringBuffer currentCode = new StringBuffer();
  93.         int index = 0;
  94.         while(index<strCode.length())
  95.         {
  96.             String code = codeMap.get(strCode.charAt(index++)+"");
  97.             currentCode.append(code);
  98.             lengthCode.append(code.length());
  99.         }
  100.         index = lengthCode.length()-1;
  101.         int start = 0;
  102.         int end = 0;
  103.         StringBuffer result = new StringBuffer();
  104.         while(index>=0)
  105.         {
  106.             end = Integer.valueOf(lengthCode.charAt(index--)+"")+start;
  107.             String ttt = currentCode.substring(start,end);
  108.             result.append(ref.get(ttt));
  109.             start = end;
  110.         }
  111.         if(flag!=0)
  112.             System.out.println();
  113.         flag++;
  114.         System.out.print(flag+": " + result.toString());
  115.     }
  116.     static int flag = 0;
  117. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值