728x90
https://www.acmicpc.net/problem/1181
메모리: 24,176 KB , 시간: 304 ms
사용 알고리즘: 정렬, 문자열
728x90
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < N; i++) { list.add(br.readLine()); } Collections.sort(list, (o1, o2) -> o1.length() == o2.length() // 길이가 같으면 ? o1.compareTo(o2) // 사전순으로 : o1.length() - o2.length()); // 길이가 다르면 짧은 것 먼저 StringBuilder result = new StringBuilder(list.get(0) + "\n"); for (int i = 1; i < N; i++) { // 중복되지 않은 단어만 정답에 넣어줌 if(!list.get(i).equals(list.get(i - 1))) result.append(list.get(i)).append("\n"); } System.out.println(result); } }
728x90
'Problem Solving > BOJ' 카테고리의 다른 글
[백준, BOJ 16971] 배열 B의 값 (java) (1) | 2024.09.24 |
---|---|
[백준, BOJ 2346] 풍선 터뜨리기 (java) (0) | 2024.09.22 |
[백준, BOJ 1996] 지뢰 찾기 (java) (0) | 2024.09.21 |
[백준, BOJ 10162] 전자레인지 (java) (1) | 2024.09.16 |
[백준, BOJ 2864] 5와 6의 차이 (java) (0) | 2024.09.16 |