Deep_Dev
article thumbnail

 


๋จผ์ € ๋ฐฐ์—ด์— ๋ชจ๋“  ์›์†Œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅํ•˜๊ณ  sort() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์—์„œ ๋ฐฐ์› ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ์ •๋ ฌ์„ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธธ์ด ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋˜๊ณ  ๊ธธ์ด๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ๋Š” ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋œ๋‹ค.

 

์ด๋•Œ ์ถœ๋ ฅํ•  ๋•Œ์—๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ์ถœ๋ ฅํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์ด ์ง์ „ ๋ฐฐ์—ด์— ์žˆ๋Š” ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์ง€ ์•Š์„ ๊ฒฝ์šฐ๋งŒ ์ถœ๋ ฅํ•˜์—ฌ ๋ฌธ์ œ์˜ ์กฐ๊ฑด๋Œ€๋กœ '์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์€ ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅ'ํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

import java.util.*;

class Main {
    public static void main(String args[]) throws Exception {
        Scanner kb = new Scanner(System.in);
        int N=kb.nextInt();
        String[] arr=new String[N];
        kb.nextLine(); // ๊ฐœํ–‰ ๋ฒ„๋ฆฌ๊ธฐ

        for(int i=0; i<N; i++) arr[i]=kb.nextLine();

        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // ๋‹จ์–ด ๊ธธ์ด๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ
                if(o1.length()==o2.length()){
                    return o1.compareTo(o2);
                }
                // ๊ทธ ์™ธ ๊ฒฝ์šฐ
                else{
                    return o1.length()-o2.length();
                }
            }
        });
        System.out.println(arr[0]);
        for(int i=1; i<N; i++){
            // ์ค‘๋ณต ํ”ผํ•˜๊ธฐ
            if(!arr[i].equals(arr[i-1])){
                System.out.println(arr[i]);
            }
        }
    }
}