poj1002 郁闷的电话号码


import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/**
* poj1002
* 题意就是输入几组字符串,按照给出的形式转换为统一格式,
* 然后输出出现多于一次的字符串出现的次数,输出时按照字典顺序排列
* 测试过这样的数据量
* 10000 18s
* 20000 116s
* 50000 等了4分多钟没结果
* 一直过不了,有时是re,有时是超时
* 改成DataInputStream居然过了,太兴奋了!!!!!!!
* 太无语了。。。。痛苦了好久啊。。。。
* @author NC
*/
public class Main {

public static char getNum(char c) {
//注意对于数字字符要用'',如果没有的话,虽然也会自转,但是只当作对应0到9的ascii码
if (Character.isDigit(c)) {
return c;
}
if (c == 'A' || c == 'B' || c == 'C') {
return '2';
}
if (c == 'D' || c == 'E' || c == 'F') {
return '3';
}
if (c == 'G' || c == 'H' || c == 'I') {
return '4';
}
if (c == 'J' || c == 'K' || c == 'L') {
return '5';
}
if (c == 'M' || c == 'N' || c == 'O') {
return '6';
}
if (c == 'P' || c == 'R' || c == 'S') {
return '7';
}
if (c == 'T' || c == 'U' || c == 'V') {
return '8';
}
if (c == 'W' || c == 'X' || c == 'Y') {
return '9';
}
return '#';
}

public static void main(String[] args) throws IOException {
// Scanner scan = new Scanner(new BufferedInputStream(System.in));
DataInputStream scan = new DataInputStream(new BufferedInputStream(System.in));
// if (scan.hasNext()) {
Map<String, Integer> tm = new TreeMap();
// int n = Integer.parseInt(scan.nextLine().trim());
int n = Integer.parseInt(scan.readLine().trim());//方法虽然过时了,但却更快
for (int i = 0; i < n; i++) {
// String s = scan.nextLine().replace("-", "");
String s = scan.readLine().replace("-", "");
StringBuilder sb = new StringBuilder();
//转字符电话号码为数字电话号码
for (int k = 0; k < s.length(); k++) {
char c = getNum(s.charAt(k));
if (Character.isDigit(c)) {
sb.append(c);
}
}
String result = sb.toString().substring(0, 3) + '-' + sb.toString().substring(3);
//统计字符串出现的次数,因为要求是字典顺序,故选用TreeMap
if (tm.containsKey(result)) {
int count = tm.get(result) + 1;
tm.put(result, count);
} else {
tm.put(result, 1);
}
}
// System.out.println("111");
Set se = tm.keySet();
Iterator it = se.iterator();
boolean flag = false;
while (it.hasNext()) {
String s = it.next().toString();
int count = tm.get(s);
if (count > 1) {
flag = true;
System.out.println(s + " " + count);
}
}
if (!flag) {
System.out.println("No duplicates. ");
}
}
// }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值