2024년 1월 5일
https://www.acmicpc.net/problem/1371
1371번: 가장 많은 글자
첫째 줄부터 글의 문장이 주어진다. 글은 최대 50개의 줄로 이루어져 있고, 각 줄은 최대 50개의 글자로 이루어져 있다. 각 줄에는 공백과 알파벳 소문자만 있다. 문장에 알파벳은 적어도 하나 이
www.acmicpc.net
내 풀이
31120kb 44ms
import sys
input_sentence = sys.stdin.read().replace("\n","").replace(' ', '')
dict = {}
for letter in input_sentence:
if dict.get(letter):
dict[letter] += 1
else:
dict[letter] = 1
answer_list = []
answer = ''
for key, value in dict.items():
if max(dict.values()) == value:
answer_list.append(key)
answer_list.sort()
answer = ''.join(answer_list)
print(answer)
메모
문자를 한 줄씩 받아 처리하면 너무 복잡해짐. 한 번에 input을 받아 처리할 수 있도록 sys 모듈 import 하여 처리
#sys.stdin.read():파일의 끝까지 한 번에 읽어오기
#sys.stdin.read().splitlines():파일의 끝까지 읽어오고 개행문자를 제외한 후 리스트
#sys.stdin.readline():'\n'의 개행문자가 포함된 한 줄만 읽어오기
#sys.stdin.readlines():'\n'의 개행문자를 포함해 끝까지 읽어오기
'파이썬 알고리즘 연습' 카테고리의 다른 글
[백준 1408번] 24 (0) | 2024.01.17 |
---|---|
[백준 1392번] 노래 악보 (0) | 2024.01.17 |
[백준 1362번] 펫 (0) | 2024.01.16 |
[백준 1350번] 진짜 공간 (0) | 2024.01.16 |
[백준 1333번] 부재중 전화 (0) | 2024.01.16 |