Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

지극히 개인적인 개발블로그

백준 알고리즘 1181: 단어 정렬(Java) 본문

알고리즘

백준 알고리즘 1181: 단어 정렬(Java)

코드분쇄기 2019. 10. 20. 13:54

문제 풀이: 배울 점이 많은 좋은 문제라고 생각합니다. 중복을 알아서 제거해주는 set이라는 자료구조와 Compare오버로딩 함수가 매우 유용했습니다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Sol_1181 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        HashSet<String> set = new HashSet<String>();

        for(int i=0; i<n; i++){
            set.add(br.readLine());
        }

        ArrayList<String> list = new ArrayList<String>(set);

        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length() > o2.length())
                    return 1;
                else if(o1.length() < o2.length())
                    return -1;
                else
                    return o1.compareTo(o2);
            }
        });

        for(String s: list)
            System.out.println(s);
    }
}