POJ 1002

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class poj1002 {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		int loop = Integer.parseInt(in.readLine().trim());
		for (int i = 0; i < loop; i++) {
			String tmp = in.readLine().trim().replaceAll("-", "");
			tmp = parse(tmp);
			// tmp = tmp.replaceAll("[A-C]", "2");
			// tmp = tmp.replaceAll("[D-F]", "3");
			// tmp = tmp.replaceAll("[G-I]", "4");
			// tmp = tmp.replaceAll("[J-L]", "5");
			// tmp = tmp.replaceAll("[M-O]", "6");
			// tmp = tmp.replaceAll("[PRS]", "7");
			// tmp = tmp.replaceAll("[TUV]", "8");
			// tmp = tmp.replaceAll("[WXY]", "9");
			String head = tmp.substring(0, 3) + "-"
					+ tmp.substring(3, tmp.length());
			Integer times = map.get(head);
			map.put(head, times == null ? 1 : times + 1);
		}
		Set<String> keys = new TreeSet<String>(map.keySet());
		boolean hasOutput = false;
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String key = it.next();
			if (map.get(key) > 1) {
				hasOutput = true;
				System.out.println(key + " " + map.get(key));
			}
		}
		if (!hasOutput) {
			System.out.println("No duplicates.");
		}
	}

	private static String parse(String tmp) {
		String result = "";
		int length = tmp.length();
		for (int i = 0; i < length; i++) {
			char ch = tmp.charAt(i);
			if (ch >= '0' && ch <= '9') {
				result += ch;
			} else {
				result += convert(ch);
			}
		}
		return result;
	}

	private static int convert(char ch) {
		if (ch >= 'A' && ch <= 'C') {
			return 2;
		} else if (ch >= 'D' && ch <= 'F') {
			return 3;
		} else if (ch >= 'G' && ch <= 'I') {
			return 4;
		} else if (ch >= 'J' && ch <= 'L') {
			return 5;
		} else if (ch >= 'M' && ch <= 'O') {
			return 6;
		} else if (ch >= 'P' && ch <= 'S' && ch != 'Q') {
			return 7;
		} else if (ch >= 'T' && ch <= 'V') {
			return 8;
		} else if (ch >= 'W' && ch <= 'Y') {
			return 9;
		} else {
			return 0;
		}
	}
}

修改后代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class poj1002 {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		int loop = Integer.parseInt(in.readLine().trim());
		for (int i = 0; i < loop; i++) {
			String tmp = in.readLine().trim().replaceAll("-", "");
			tmp = parse(tmp);
			// tmp = tmp.replaceAll("[A-C]", "2");
			// tmp = tmp.replaceAll("[D-F]", "3");
			// tmp = tmp.replaceAll("[G-I]", "4");
			// tmp = tmp.replaceAll("[J-L]", "5");
			// tmp = tmp.replaceAll("[M-O]", "6");
			// tmp = tmp.replaceAll("[PRS]", "7");
			// tmp = tmp.replaceAll("[TUV]", "8");
			// tmp = tmp.replaceAll("[WXY]", "9");
//			String head = tmp.substring(0, 3) + "-"
//					+ tmp.substring(3, tmp.length());
			Integer times = map.get(tmp);
			map.put(tmp, times == null ? 1 : times + 1);
		}
		Set<String> keys = new TreeSet<String>(map.keySet());
		boolean hasOutput = false;
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String key = it.next();
			if (map.get(key) > 1) {
				hasOutput = true;
				System.out.println(key.substring(0, 3) + "-"
						+ key.substring(3, key.length()) + " " + map.get(key));
			}
		}
		if (!hasOutput) {
			System.out.println("No duplicates.");
		}
	}

	private static String parse(String tmp) {
		String result = "";
		int length = tmp.length();
		for (int i = 0; i < length; i++) {
			char ch = tmp.charAt(i);
			if (ch >= '0' && ch <= '9') {
				result += ch;
			} else {
				result += convert(ch);
			}
		}
		return result;
	}

	private static int convert(char ch) {
//		if (ch >= 'A' && ch <= 'C') {
//			return 2;
//		} else if (ch >= 'D' && ch <= 'F') {
//			return 3;
//		} else if (ch >= 'G' && ch <= 'I') {
//			return 4;
//		} else if (ch >= 'J' && ch <= 'L') {
//			return 5;
//		} else if (ch >= 'M' && ch <= 'O') {
//			return 6;
//		} else if (ch >= 'P' && ch <= 'S' && ch != 'Q') {
//			return 7;
//		} else if (ch >= 'T' && ch <= 'V') {
//			return 8;
//		} else if (ch >= 'W' && ch <= 'Y') {
//			return 9;
//		} else {
//			return 0;
//		}
		if(ch=='Q'||ch=='Z'){
			return 0;
		}
		if(ch>'Q'){
			ch -= 1;
		}
		return (ch - 'A')/3+2;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值