사용 알고리즘: set, 우선순위 큐
사용 언어: java
package week31_32;
import java.util.*;
import java.io.*;
public class Solution_swea_7701_염라대왕의이름정렬 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T =Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
int n = Integer.parseInt(br.readLine());
Set<String> set = new HashSet<>();
for (int i = 0; i < n; i++) {
set.add(br.readLine());
}
Queue<String> q = new PriorityQueue<>((a,b) -> {
if(a.length() != b.length())
return a.length() - b.length();
return a.compareTo(b); //으악
});
for (String s : set) {
q.add(s);
}
System.out.println("#"+t);
while(!q.isEmpty()) {
System.out.println(q.poll());
}
}
}
}
어렵지는 않았지만, String을 compare하는 방법은 외워두기!
Queue<String> q = new PriorityQueue<>((a,b) -> {
if(a.length() != b.length())
return a.length() - b.length();
return a.compareTo(b); //으악
});
'알고리즘 > swea' 카테고리의 다른 글
1249_보급로 (0) | 2025.01.09 |
---|---|
6730. 장애물 경주 난이도 (0) | 2024.07.08 |
8931. 제로 (0) | 2024.07.07 |
6808. 규영이와 인영이의 카드게임 (0) | 2024.07.07 |