BOJ 9872 - Record Keeping (Python)

조민수·2024년 6월 23일
0

BOJ

목록 보기
61/64
post-custom-banner

S3, 문자열


문제

Farmer John has been keeping detailed records of his cows as they enter the barn for milking. Each hour, a group of 3 cows enters the barn, and Farmer John writes down their names. For example over a 5-hour period, he might write down the following list, where each row corresponds to a group entering the barn:

BESSIE ELSIE MATILDA FRAN BESSIE INGRID BESSIE ELSIE MATILDA MATILDA INGRID FRAN ELSIE BESSIE MATILDA

Farmer John notes that the same group of cows may appear several times on his list; in the example above, the group of BESSIE, ELSIE, and MATILDA appears three times (even though Farmer John didn't necessarily write their names in the same order every time they entered the barn).

Please help Farmer John count the number of occurrences of the group entering the barn the most.


풀이

  • 각 값당 개수를 세는 문제가 아니라 들어오는 집단의 수를 세는 문제
  • 딕셔너리의 key는 리스트가 될 수 없다. mutable하기 때문
    • tuple은 내부 값을 수정/추가 할 수 없기 때문에 immutable하다.
from sys import stdin
from collections import defaultdict
n = int(stdin.readline())
total = defaultdict(int)
for _ in range(n):
    lst = tuple(sorted(list(map(str, stdin.readline().split()))))
    total[lst] += 1
    
tmp = sorted(total.items(), key = lambda x : x[1], reverse= True)
print(tmp[0][1])
profile
사람을 좋아하는 Front-End 개발자
post-custom-banner

0개의 댓글