๐งฉ Baekjoon 1181 - ๋จ์ด ์ ๋ ฌ
๐งฉ Baekjoon 1181 - ๋จ์ด ์ ๋ ฌ
๋ฌธ์
์ํ๋ฒณ ์๋ฌธ์๋ก ์ด๋ฃจ์ด์ง N๊ฐ์ ๋จ์ด๊ฐ ๋ค์ด์ค๋ฉด ์๋์ ๊ฐ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ ๋ ฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
- ๊ธธ์ด๊ฐ ์งง์ ๊ฒ๋ถํฐ
- ๊ธธ์ด๊ฐ ๊ฐ์ผ๋ฉด ์ฌ์ ์์ผ๋ก
๋จ, ์ค๋ณต๋ ๋จ์ด๋ ํ๋๋ง ๋จ๊ธฐ๊ณ ์ ๊ฑฐํด์ผ ํ๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ๋จ์ด์ ๊ฐ์ N์ด ์ฃผ์ด์ง๋ค. (1 โค N โค 20,000) ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์ ๊ฑธ์ณ ์ํ๋ฒณ ์๋ฌธ์๋ก ์ด๋ฃจ์ด์ง ๋จ์ด๊ฐ ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ 50์ ๋์ง ์๋๋ค.
์ถ๋ ฅ
์กฐ๊ฑด์ ๋ฐ๋ผ ์ ๋ ฌํ์ฌ ๋จ์ด๋ค์ ์ถ๋ ฅํ๋ค.
์์
โ ์ ๋ ฅ 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
yours
โ ์ถ๋ ฅ 1
1
2
3
4
5
6
7
8
9
10
11
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate
์์ฑ ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) throws IOException {
// 1. ๋ณ์ ์ ์ธ ๋ฐ ์ด๊ธฐํ
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] words = new String[n];
// 2. ๋จ์ด ๋ชฉ๋ก ์ด๊ธฐํ
for (int i = 0; i < n; i++) words[i] = br.readLine();
// 3. ์คํธ๋ฆผ ์ฒ๋ฆฌ
words = Arrays.stream(words)
.sorted(
Comparator.comparingInt(String::length) // ๊ธธ์ด์ ์ ๋ ฌ
.thenComparing(Comparator.naturalOrder()) // ์ฌ์ ์ ์ ๋ ฌ
)
.distinct() // ์ค๋ณต ์ ๊ฑฐ
.toArray(String[]::new);
// 4. ์ถ๋ ฅ
for (String word : words) System.out.println(word);
}
}
Stream
์ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ์ด๋ป๊ฒ ๊ตฌํํ ์ ์์์ง ์๊ฐํด๋ณด์์ผ๊ฒ ๋ค.
This post is licensed under CC BY 4.0 by the author.