https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
HashSet<String> temp = new HashSet<>();
for(int i=0; i<n; i++){
temp.add(br.readLine());
}
String[] a = new String[temp.size()];
Iterator iter = temp.iterator();
int idx =0;
while(iter.hasNext()) {
a[idx++] = (String)iter.next();
}
br.close();
Arrays.sort(a,new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
if(str1.length()<str2.length()) {
return -1;
}
else if(str1.length()==str2.length()) {
return str1.compareTo(str2);
}
else return 1;
}
});
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i=0; i<a.length; i++) {
bw.write(a[i]+"\n");
}
bw.close();
}
}
'백준' 카테고리의 다른 글
백준 정렬 - 18870번 : 좌표 압축 (0) | 2022.02.10 |
---|---|
백준 정렬 - 10814번 : 나이순 정렬 (0) | 2022.02.09 |
백준 정렬 - 11651번 : 좌표 정렬하기 2 (0) | 2022.02.09 |
백준 정렬 - 11650번 : 좌표 정렬하기 (0) | 2022.02.09 |
백준 정렬 - 2108번 : 통계학 (0) | 2022.02.08 |