PKU_ACM_1051_ P,MTHBGWB
2008-11-06 11:31
225 查看
P,MTHBGWB
原题连接
简单的字符匹配的问题,很容易
import java.io.*;
import java.util.*;
public class Main
{
static HashMap<String,String> codeMap = new HashMap<String,String>();
static HashMap<String,String> ref = new HashMap<String,String>();
public static void main(String[] args) throws Exception
{
initMap();
readFile();
}
static void readFile() throws Exception
{
BufferedReader br = new BufferedReader(
new FileReader("in.in"));
//new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
int count = 0;
while(count<n)
{
process(br.readLine());
count++;
}
}
static void initMap()
{
codeMap.put("A",".-");
codeMap.put("B","-...");
codeMap.put("C","-.-.");
codeMap.put("D","-..");
codeMap.put("E",".");
codeMap.put("F","..-.");
codeMap.put("G","--.");
codeMap.put("H","....");
codeMap.put("I","..");
codeMap.put("J",".---");
codeMap.put("K","-.-");
codeMap.put("L",".-..");
codeMap.put("M","--");
codeMap.put("N","-.");
codeMap.put("O","---");
codeMap.put("P",".--.");
codeMap.put("Q","--.-");
codeMap.put("R",".-.");
codeMap.put("S","...");
codeMap.put("T","-");
codeMap.put("U","..-");
codeMap.put("V","...-");
codeMap.put("W",".--");
codeMap.put("X","-..-");
codeMap.put("Y","-.--");
codeMap.put("Z","--..");
codeMap.put("_","..--");
codeMap.put(".","---.");
codeMap.put(",",".-.-");
codeMap.put("?","----");
ref.put(".-","A");
ref.put("-...","B");
ref.put("-.-.","C");
ref.put("-..","D");
ref.put(".","E");
ref.put("..-.","F");
ref.put("--.","G");
ref.put("....","H");
ref.put("..","I");
ref.put(".---","J");
ref.put("-.-","K");
ref.put(".-..","L");
ref.put("--","M");
ref.put("-.","N");
ref.put("---","O");
ref.put(".--.","P");
ref.put("--.-","Q");
ref.put(".-.","R");
ref.put("...","S");
ref.put("-","T");
ref.put("..-","U");
ref.put("...-","V");
ref.put(".--","W");
ref.put("-..-","X");
ref.put("-.--","Y");
ref.put("--..","Z");
ref.put("..--","_");
ref.put("---.",".");
ref.put(".-.-",",");
ref.put("----","?");
}
static void process(String str)
{
StringBuffer strCode = new StringBuffer(str);
StringBuffer lengthCode = new StringBuffer();
StringBuffer currentCode = new StringBuffer();
int index = 0;
while(index<strCode.length())
{
String code = codeMap.get(strCode.charAt(index++)+"");
currentCode.append(code);
lengthCode.append(code.length());
}
index = lengthCode.length()-1;
int start = 0;
int end = 0;
StringBuffer result = new StringBuffer();
while(index>=0)
{
end = Integer.valueOf(lengthCode.charAt(index--)+"")+start;
String ttt = currentCode.substring(start,end);
result.append(ref.get(ttt));
start = end;
}
if(flag!=0)
System.out.println();
flag++;
System.out.print(flag+": " + result.toString());
}
static int flag = 0;
}
原题连接
简单的字符匹配的问题,很容易
import java.io.*;
import java.util.*;
public class Main
{
static HashMap<String,String> codeMap = new HashMap<String,String>();
static HashMap<String,String> ref = new HashMap<String,String>();
public static void main(String[] args) throws Exception
{
initMap();
readFile();
}
static void readFile() throws Exception
{
BufferedReader br = new BufferedReader(
new FileReader("in.in"));
//new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
int count = 0;
while(count<n)
{
process(br.readLine());
count++;
}
}
static void initMap()
{
codeMap.put("A",".-");
codeMap.put("B","-...");
codeMap.put("C","-.-.");
codeMap.put("D","-..");
codeMap.put("E",".");
codeMap.put("F","..-.");
codeMap.put("G","--.");
codeMap.put("H","....");
codeMap.put("I","..");
codeMap.put("J",".---");
codeMap.put("K","-.-");
codeMap.put("L",".-..");
codeMap.put("M","--");
codeMap.put("N","-.");
codeMap.put("O","---");
codeMap.put("P",".--.");
codeMap.put("Q","--.-");
codeMap.put("R",".-.");
codeMap.put("S","...");
codeMap.put("T","-");
codeMap.put("U","..-");
codeMap.put("V","...-");
codeMap.put("W",".--");
codeMap.put("X","-..-");
codeMap.put("Y","-.--");
codeMap.put("Z","--..");
codeMap.put("_","..--");
codeMap.put(".","---.");
codeMap.put(",",".-.-");
codeMap.put("?","----");
ref.put(".-","A");
ref.put("-...","B");
ref.put("-.-.","C");
ref.put("-..","D");
ref.put(".","E");
ref.put("..-.","F");
ref.put("--.","G");
ref.put("....","H");
ref.put("..","I");
ref.put(".---","J");
ref.put("-.-","K");
ref.put(".-..","L");
ref.put("--","M");
ref.put("-.","N");
ref.put("---","O");
ref.put(".--.","P");
ref.put("--.-","Q");
ref.put(".-.","R");
ref.put("...","S");
ref.put("-","T");
ref.put("..-","U");
ref.put("...-","V");
ref.put(".--","W");
ref.put("-..-","X");
ref.put("-.--","Y");
ref.put("--..","Z");
ref.put("..--","_");
ref.put("---.",".");
ref.put(".-.-",",");
ref.put("----","?");
}
static void process(String str)
{
StringBuffer strCode = new StringBuffer(str);
StringBuffer lengthCode = new StringBuffer();
StringBuffer currentCode = new StringBuffer();
int index = 0;
while(index<strCode.length())
{
String code = codeMap.get(strCode.charAt(index++)+"");
currentCode.append(code);
lengthCode.append(code.length());
}
index = lengthCode.length()-1;
int start = 0;
int end = 0;
StringBuffer result = new StringBuffer();
while(index>=0)
{
end = Integer.valueOf(lengthCode.charAt(index--)+"")+start;
String ttt = currentCode.substring(start,end);
result.append(ref.get(ttt));
start = end;
}
if(flag!=0)
System.out.println();
flag++;
System.out.print(flag+": " + result.toString());
}
static int flag = 0;
}
相关文章推荐
- PKU-1051 P,MTHBGWB (map的使用)
- POJ 1051 P,MTHBGWB 已被翻译
- POJ 1051 P,MTHBGWB 简单字符串转换
- 1051 P,MTHBGWB
- P,MTHBGWB - 1051
- POJ 1051 P,MTHBGWB 笔记
- poj 1051 P,MTHBGWB
- 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB(map模拟)
- poj 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- POJ 1051 P,MTHBGWB
- poj 1051 P,MTHBGWB (模拟题字符串 )
- POJ-1051-P,MTHBGWB
- POJ 1051 P,MTHBGWB
- 基础题训练(简单数学题、字符串)P,MTHBGWB POJ-1051
- POJ 1051: P,MTHBGWB
- zoj1068P,MTHBGWB
- ZOJ 1068 P,MTHBGWB
- zoj1068 P,MTHBGWB