https://www.acmicpc.net/problem/20920

📔문제

화은이는 이번 영어 시험에서 틀린 문제를 바탕으로 영어 단어 암기를 하려고 한다. 그 과정에서 효율적으로 영어 단어를 외우기 위해 영어 단어장을 만들려 하고 있다. 화은이가 만들고자 하는 단어장의 단어 순서는 다음과 같은 우선순위를 차례로 적용하여 만들어진다.

  1. 자주 나오는 단어일수록 앞에 배치한다.
  2. 해당 단어의 길이가 길수록 앞에 배치한다.
  3. 알파벳 사전 순으로 앞에 있는 단어일수록 앞에 배치한다

MM보다 짧은 길이의 단어의 경우 읽는 것만으로도 외울 수 있기 때문에 길이가 MM이상인 단어들만 외운다고 한다. 화은이가 괴로운 영단어 암기를 효율적으로 할 수 있도록 단어장을 만들어 주자.


📝입력

첫째 줄에는 영어 지문에 나오는 단어의 개수 NN과 외울 단어의 길이 기준이 되는 MM이 공백으로 구분되어 주어진다. (1N1000001 \leq N \leq 100\,000, 1M101 \leq M \leq 10)

둘째 줄부터 N+1N+1번째 줄까지 외울 단어를 입력받는다. 이때의 입력은 알파벳 소문자로만 주어지며 단어의 길이는 1010을 넘지 않는다.

단어장에 단어가 반드시 1개 이상 존재하는 입력만 주어진다.


📺출력

화은이의 단어장에 들어 있는 단어를 단어장의 앞에 위치한 단어부터 한 줄에 한 단어씩 순서대로 출력한다.


📝예제 입력 1

7 4
apple
ant
sand
apple
append
sand
sand

📺예제 출력 1

sand
apple
append

📝예제 입력 2

12 5
appearance
append
attendance
swim
swift
swift
swift
mouse
wallet
mouse
ice
age

📺예제 출력 2

swift
mouse
appearance
attendance
append
wallet

🔍출처

Camp > ICPC Sinchon Algorithm Camp > 2021 ICPC Sinchon Winter Algorithm Camp Contest > 초급 A번
-문제를 검수한 사람: gratus907, iknoom1107, jwvg0425, tony9402
-문제를 만든 사람: whaeun25


🧮알고리즘 분류

  • 자료구조
  • 문자열
  • 정렬
  • 해시를 사용한 집합과 맵
  • 트리를 사용한 집합과 맵

📃소스 코드

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Code20920 {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] temp=br.readLine().split(" ");

        int N=Integer.valueOf(temp[0]); //나올 단어 수
        int M=Integer.valueOf(temp[1]); //외울 단어 길이 (얘보다 길거나 같아야 외움)

//        ArrayList<voca> vocas=new ArrayList<>();
        HashMap<String,Integer> map=new HashMap<>();
//        HashSet<String> t=new HashSet<>();

        for(int i=0;i<N;i++){
            String str=br.readLine();
            if(str.length()>=M){
                int n=map.getOrDefault(str,0);
                map.put(str,n+1);
//                t.add(str);
            }
        }
        br.close();

        List<String> voca=map.keySet().stream().collect(Collectors.toList());

        voca.sort((o1,o2)->{
            int a=map.get(o1);
            int b=map.get(o2);

            if(a==b){
                if(o1.length()==o2.length()){
                    return o1.compareTo(o2);//사전순
                }
                return o2.length()-o1.length();
            }
            return b-a;
        });



//        ArrayList<String> names=new ArrayList<>(t);
//
//
//        for(int i=0;i<map.size();i++){
//            vocas.add(new voca(names.get(i),map.get(names.get(i)),names.get(i).length()));
//        }

//        Comparator<voca> lens=Comparator.comparing(voca::getLen).reversed();
//        Comparator<voca> words=Comparator.comparing(voca::getWord);
//        vocas.sort(Comparator.comparing(voca::getCount).reversed().thenComparing(lens).thenComparing(words));
//
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
//        for(voca v:vocas){
//            bw.write(v.getWord()+"\n");
//        }

        for(int i=0;i<voca.size();i++){
            bw.write(voca.get(i)+"\n");
        }
        bw.flush();
        bw.close();
    }
}

//class voca{
//    String word;
//    int count;
//    int len;
//
//    public int getCount(){
//        return count;
//    }
//    public int getLen(){
//        return len;
//    }
//    public String getWord(){
//        return word;
//    }
//    public voca(String word, int count, int len){
//        this.word=word;
//        this.count=count;
//        this.len=len;
//    }
//}


📰출력 결과


📂고찰

처음에 ArrayList로 정렬을 했는데, class도 사용하고.. 또 시간초과가 떴다. 그래서 해시맵을 여러가지 방법으로 정렬하는 걸 찾아보았다.

List<String> voca=map.keySet().stream().collect(Collectors.toList());

        voca.sort((o1,o2)->{
            int a=map.get(o1);
            int b=map.get(o2);

            if(a==b){
                if(o1.length()==o2.length()){
                    return o1.compareTo(o2);//사전순
                }
                return o2.length()-o1.length();
            }
            return b-a;
        });

정렬하는 공부를 좀 더 해야할것같다.

profile
MySQL DBA 신입 지원

0개의 댓글