문제 출처 : https://www.acmicpc.net/problem/9612
언어 : Kotlin
문제 설명 :
Term frequency–Inverse document frequency (tf-idf) is a numerical statistic which reflects the importance of words in a document collection. It is often used in information retrieval system. The number of times a word appears in the document (word frequency) is one of the major factors to acquire tf-idf.
You are asked to write a program to find the most frequent word in a document.
입력 :
The first line contains an integer n (1≤ n ≤ 1000) which determines the number of words. The following n lines include the list of words, one word per line. A word contains only lower-case letters and it can contain up to 20 characters.
출력 :
Print out the word that has the highest frequency and its frequency, separated by a single space. If you get more than 2 results, choose only the one that comes later in the lexicographical order.
제한 사항 :
- 시간 제한 : 1초
- 메모리 제한 : 128MB
입출력 예 :
입력 | 출력 |
10 mountain lake lake zebra tree lake zebra zebra animal lakes |
zebra 3 |
풀이 :
import java.io.BufferedWriter
import java.io.OutputStreamWriter
fun main() = with(System.`in`.bufferedReader()) {
val bw = BufferedWriter(OutputStreamWriter(System.out))
val words = Array<String>(readLine().toInt()) { readLine() }
.groupingBy { it }
.eachCount()
.maxBy { it.key }
bw.write("${words.key} ${words.value}")
bw.flush()
bw.close()
}
'백준 > 문제' 카테고리의 다른 글
1148번: 단어 만들기 (0) | 2024.08.29 |
---|---|
8387번: Dyslexia (1) | 2024.08.29 |
1622번: 공통 순열 (0) | 2024.08.28 |
22950번: 이진수 나눗셈 (0) | 2024.08.27 |
16944번: 강력한 비밀번호 (0) | 2024.08.27 |
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!