시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 512 MB93464162.121%

문제

Ben heard about studies by Emmons and McCullough that suggest that intentionally practicing gratitude has a lasting effect on people's happiness. Since he wants to be happy too, he decided that at the end of each day he will think back over the past day and write down three things he is thankful for, one thing per line. At the end of $N$ days in which he practiced this exercise, he was curious to know which things appear the most on his list. Help Ben get the $K$ things he was grateful for most frequently.

입력

The input begins with one line containing two space-separated integers, $N$ and $K$, in that order. Then follow $3N$ lines containing Ben's notes from $N$ days. You may assume that the three lines that correspond to the same day contain no repetitions. That is, if you partition the input into $N$ chunks of $3$ consecutive lines, no chunk contains two identical lines.

출력

The output should represent the list of things that Ben is grateful for, ordered by frequency of appearance in Ben's list (with the most frequent item first). In case of two items with equal frequency, the most recent item should appear first. That is, in case of a tie in the number of appearances, the item whose last appearance is later in the input should appear earlier in the output. Finally, if there are more than $K$ different items in Ben's list, your output should contain only the $K$ first items (according to the required order).

제한

  • $1 \le K \le 3N \le 100\,000$
  • Each input line contains at most $50$ (ASCII) characters.

예제 입력 1

2 2
Supportive parents
Being able to solve a hard problem
Good food
Fun game with friends
Good food
Being healthy

예제 출력 1

Good food
Being healthy

Good food is the only item that appears twice in Ben's list, so it should appear first in the output. All other items appear once in the input, but Being healthy takes precedence as it is the most recent.

예제 입력 2

2 6
Supportive parents
Being able to solve a hard problem
Good food
Fun game with friends
Good food
Being healthy

예제 출력 2

Good food
Being healthy
Fun game with friends
Being able to solve a hard problem
Supportive parents

Here there are only $5$ different items that Ben is grateful for, so there are only $5$ lines of output. In this list, Good food is first in the output since it appears twice in the input, and the other items are ordered by last appearance in Ben's list.

출처

ICPC > Regionals > Europe > Southwestern European Regional Contest > SWERC 2020-2021 A번

  • 문제를 만든 사람: Nofar Carmeli