📢 공지합니다
이 게시글은 메인 페이지에 항상 고정되어 표시됩니다.
https://www.acmicpc.net/problem/20920
package datastructure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class DataStructure20920 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < n; i++) {
String word2 = br.readLine();
if (word2.length() >= m) {
// 중복 체크 최적화
hashMap.put(word2, hashMap.getOrDefault(word2, 0) + 1);
}
}
// 1. HashMap을 List로 변환
List<Map.Entry<String, Integer>> list = new ArrayList<>(hashMap.entrySet());
list.sort((e1,e2)-> {
int count1 = e1.getValue();
int count2= e2.getValue();
String word1 = e1.getKey();
String word2 = e2.getKey();
if(count1 != count2){
return count2 - count1;
}
if(word2.length() != word1.length()){
return word2.length()- word1.length();
}
return word1.compareTo(word2);
});
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Integer> entry : list) {
sb.append(entry.getKey()).append("\n");
}
System.out.print(sb);
}
}
서류 붙어서 지금 코테준비하느라 시간이없다 ㅏㅏㅏㅏㅏㅏ
map을 오랜만에 풀어본다
[백준 2607번] 비슷한 단어 (0) | 2024.12.13 |
---|---|
[백준 2512번] 예산 (1) | 2024.12.12 |
[백준] 탑 2493번 (🥇 골드5) (0) | 2024.08.23 |
[백준] 택배 배송 5972번 (🥇 골드5) (0) | 2024.08.06 |
[백준] 리모컨 1107번 (🥇 골드5) (1) | 2024.07.03 |