코테풀이
[백준] BOJ - 20920 영단어 암기는 괴로워 자바 java (실버3)
헝D
2023. 2. 15. 22:58
https://www.acmicpc.net/problem/20920
20920번: 영단어 암기는 괴로워
첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단
www.acmicpc.net
나의 풀이
import java.util.*;
import java.io.*;
import java.util.stream.*;
public class Main {
public static void main(String args[]) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
Map<String, Integer> map=new HashMap<>();
for(int i=0;i<n;i++){
String temp=br.readLine();
if(temp.length() < m){
continue;
}
if(map.get(temp) == null){// 처음 나온 값
map.put(temp,1);
}else{//있던 값이면 개수를 추가
map.replace(temp,map.get(temp)+1);
}
}
List<String> words = map.keySet().stream().collect(Collectors.toList());//리스트로 형변환
words.sort((o1, o2) -> {
int c1=map.get(o1);
int c2=map.get(o2);
if(c1==c2){//빈도수가 같으면
if(o1.length() == o2.length()) {//길이도 같으면
return o1.compareTo(o2); // 알파벳 오름차순
}
return o2.length()-o1.length(); // 내림차순
}
return c2-c1; // 내림차순
});
for(String s:words){
bw.write(s+"\n");
}
bw.flush();
br.close();
bw.close();
}
}