화은이는 이번 영어 시험에서 틀린 문제를 바탕으로 영어 단어 암기를 하려고 한다. 그 과정에서 효율적으로 영어 단어를 외우기 위해 영어 단어장을 만들려 하고 있다. 화은이가 만들고자 하는 단어장의 단어 순서는 다음과 같은 우선순위를 차례로 적용하여 만들어진다.
보다 짧은 길이의 단어의 경우 읽는 것만으로도 외울 수 있기 때문에 길이가 이상인 단어들만 외운다고 한다. 화은이가 괴로운 영단어 암기를 효율적으로 할 수 있도록 단어장을 만들어 주자.
첫째 줄에는 영어 지문에 나오는 단어의 개수 과 외울 단어의 길이 기준이 되는 이 공백으로 구분되어 주어진다. (, )
둘째 줄부터 번째 줄까지 외울 단어를 입력받는다. 이때의 입력은 알파벳 소문자로만 주어지며 단어의 길이는 을 넘지 않는다.
단어장에 단어가 반드시 1개 이상 존재하는 입력만 주어진다.
화은이의 단어장에 들어 있는 단어를 단어장의 앞에 위치한 단어부터 한 줄에 한 단어씩 순서대로 출력한다.
7 4
apple
ant
sand
apple
append
sand
sand
sand
apple
append
12 5
appearance
append
attendance
swim
swift
swift
swift
mouse
wallet
mouse
ice
age
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;
});
정렬
하는 공부를 좀 더 해야할것같다.