https://www.acmicpc.net/problem/1599
ng를 하나의 문자로 처리하는 것과
ang, angle 처럼 한 단어가 prefix 부분집합이 되는 경우는 더 짧은 것이 우선순위가 높음에 주의
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Main {
//a b k d e g h i l m n ng o p r s t u w y
public static void main(String[] args) throws IOException {
Map<String, Integer> dic = new HashMap<>();
dic.put("a", 1);
dic.put("b", 2);
dic.put("k", 3);
dic.put("d", 4);
dic.put("e", 5);
dic.put("g", 6);
dic.put("h", 7);
dic.put("i", 8);
dic.put("l", 9);
dic.put("m", 10);
dic.put("n", 11);
dic.put("ng", 12);
dic.put("o", 13);
dic.put("p", 14);
dic.put("r", 15);
dic.put("s", 16);
dic.put("t", 17);
dic.put("u", 18);
dic.put("w", 19);
dic.put("y", 20);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
List<String> l = new ArrayList<>();
for (int i = 0; i < N; i++) {
l.add(br.readLine());
}
Collections.sort(l, (a, b) -> {
int A = a.length();
int B = b.length();
int ai = 0;
int bi = 0;
while (ai < A && bi < B) {
int as, bs;
if (a.charAt(ai) == 'n' && ai < A - 1 && a.charAt(ai + 1) == 'g') {
as = dic.get("ng");
ai++;
} else {
as = dic.get(String.valueOf(a.charAt(ai)));
}
ai++;
if (b.charAt(bi) == 'n' && bi < B - 1 && b.charAt(bi + 1) == 'g') {
bs = dic.get("ng");
bi++;
} else {
bs = dic.get(String.valueOf(b.charAt(bi)));
}
bi++;
if (as != bs) {
return as - bs;
}
}
return (A - ai) - (B - bi);
});
l.stream().forEach(System.out::println);
}
}
'[백준] PS > Java' 카테고리의 다른 글
[백준 1647] 도시 분할 계획 - JAVA (0) | 2025.03.04 |
---|---|
[백준 1157] 도로의 개수 - JAVA (0) | 2025.03.02 |
[백준 1988] 낮잠 시간 - JAVA (0) | 2025.02.27 |
[백준 1584] 게임 - JAVA (0) | 2025.02.27 |
[백준 1937] 욕심쟁이 판다 - JAVA (0) | 2025.02.25 |