Skip to content

Commit 79ed4ea

Browse files
committed
issue #40 1181 단어정렬
1 parent 4595571 commit 79ed4ea

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/backjoon/_1181.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/1181
3+
// 단어 정렬
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.Arrays;
8+
import java.util.Comparator;
9+
10+
public class _1181 {
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
// memory 28808 runtime 288
15+
int N = Integer.parseInt(br.readLine());
16+
// 배열에 넣기
17+
String[] arr = new String[N];
18+
for (int i = 0; i < N; i++) {
19+
arr[i] = br.readLine();
20+
}
21+
22+
Arrays.sort(arr, new Comparator<String>() {
23+
@Override
24+
public int compare(String o1, String o2) {
25+
// 단어 길이가 같으면 사전순
26+
if(o1.length() == o2.length()){
27+
return o1.compareTo(o2);
28+
} else{
29+
return o1.length() - o2.length(); //길이차가 양수면 자리가 바뀌고, 음수면 그대로
30+
}
31+
}
32+
});
33+
34+
StringBuilder sb = new StringBuilder();
35+
36+
//중복되는 단어는 빼고 출력하기위해 비교를 위해 첫번째 단어를 넣어둠
37+
sb.append(arr[0]).append("\n");
38+
39+
for(int i=1; i<N; i++){
40+
if(!arr[i].equals(arr[i-1])){
41+
sb.append(arr[i]).append("\n");
42+
}
43+
}
44+
System.out.println(sb);
45+
}
46+
}
47+
/*
48+
input
49+
13
50+
but
51+
i
52+
wont
53+
hesitate
54+
no
55+
more
56+
no
57+
more
58+
it
59+
cannot
60+
wait
61+
im
62+
yours
63+
64+
output
65+
i
66+
im
67+
it
68+
no
69+
but
70+
more
71+
wait
72+
wont
73+
yours
74+
cannot
75+
hesitate
76+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy