这道题模拟即可,几点注意:
- 使用Map用TreeMap, 自带排序功能以减少使用Heap
- 补齐0不要用String.format太慢
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
static int[] table = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
static String[] leadingZero= {"000000", "00000", "0000", "000", "00", "0", ""};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.nextLine();
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < N; ++i) {
String raw = scanner.nextLine();
int val = getNumber(raw);
if (map.containsKey(val)) {
map.replace(val, map.get(val) + 1);
} else {
map.put(val, 1);
}
}
boolean flag = false;
for (Integer x : map.keySet()) {
int y = map.get(x);
if (y > 1) {
flag = true;
String s = String.valueOf(x);
s = leadingZero[s.length() - 1] + s;
System.out.println(s.substring(0, 3) + '-' + s.substring(3, 7) + " " + y);
}
}
if (!flag) System.out.println("No duplicates.");
}
static int getNumber (String raw) {
int res = 0;
for (int i = 0; i < raw.length(); ++i) {
char ch = raw.charAt(i);
if (ch == '-') continue;
if (Character.isDigit(ch)) {
res = res * 10 + (ch - '0');
} else {
res = res * 10 + table[ch - 'A'];
}
}
return res;
}
}
以下是错误示范
TLE
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
static char[] table = {'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.nextLine();
HashMap<String, Integer> map = new HashMap<>();
PriorityQueue<String> queue = new PriorityQueue<>();
for (int i = 0; i < N; ++i) {
String s = getNumber(scanner.nextLine());
if (map.containsKey(s)) {
map.replace(s, map.get(s) + 1);
} else {
map.put(s, 1);
queue.add(s);
}
}
if (queue.size() == N) {
System.out.println("No duplicates.");
} else {
while (!queue.isEmpty()) {
String s = queue.poll();
int k = map.get(s);
if (k >= 2)
System.out.println(s + " " + map.get(s));
}
}
}
private static String getNumber(String raw) {
char[] seq = new char[8];
int t = 0;
for (int i = 0; i < raw.length(); ++i) {
char ch = raw.charAt(i);
if (ch == '-') continue;
if (Character.isLetter(ch)) {
seq[t++] = table[ch - 'A'];
} else {
seq[t++] = ch;
}
if (t == 3) seq[t++] = '-';
}
return String.valueOf(seq);
}
}
Runtime Error (The array might be too large)
import java.util.Scanner;
public class Main {
static int[] table = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
static int[] count = new int[7777779];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < N; ++i) {
int x = 0;
String raw = scanner.nextLine();
for (int j = 0; j < raw.length(); ++j) {
char ch = raw.charAt(j);
if (ch == '-') continue;
if (Character.isLetter(ch)) {
x = x * 10 + table[ch - 'A'];
} else {
x = x * 10 + (ch - '0');
}
}
++count[x - 2222222];
}
boolean flag = false;
for (int i = 0; i < count.length; ++i) {
if (count[i] >= 2) {
int pos = i + 2222222;
StringBuffer buffer = new StringBuffer(String.valueOf(pos));
buffer.insert(3, "-");
System.out.println(buffer.toString() + " " + count[i]);
flag = true;
}
}
if (!flag) {
System.out.println("No duplicates.");
}
}
static String getFormatNumber (int pos) {
pos += 2222222;
StringBuffer buffer = new StringBuffer(String.valueOf(pos));
buffer.insert(3, "-");
return buffer.toString();
}
}
网友评论