본문 바로가기
Algorithm

백준 19948번 : 음유시인 영재

by jun_code 2022. 1. 25.

<문제>

링크 : https://www.acmicpc.net/problem/19948

 

19948번: 음유시인 영재

감수성이 뛰어난 음유시인 영재는 일상생활 중에 번뜩 시상이 떠오르곤 한다. 하지만 기억력이 좋지 못한 영재는 시상이 떠오르면 그 순간 컴퓨터로 기록해야만 안 까먹는다! 시는 대문자, 소

www.acmicpc.net


<코드>

import sys
input = sys.stdin.readline

poem = input().strip().split()
space = int(input())
alpha = list(map(int, input().split()))
result = True

if len(poem)-1 > space:
    result = False
else:
    s = len(poem)
    for i in range(s):
        string_i = poem[i]
        for j in range(len(string_i)):
            num = ord(string_i[j].lower())-97
            alpha[num] -= 1
            if j>=1 and (string_i[j] == string_i[j-1]):
                alpha[num] += 1
            if alpha[num] <0:
                result = False
    if result == True:
        string = ''
        for i in range(s):
            string = string + poem[i][0].upper()
        for i in range(len(string)):
            num = ord(string[i].lower())-97
            alpha[num] -= 1
            if i>=1 and (string[i] == string[i-1]):
                alpha[num] += 1
            if alpha[num] <0:
                result = False
if result == True:
    print(string)
else:
    print(-1)

<풀이>

  • 먼저 space바 사용이 가능한지를 파악하기 위해 split()을 통해 몇 번의 space바가 사용되는지 파악한다.
  • 다음으로 나눈 각 문자에 대해 ord를 통해 알파벳을 숫자로 변환하고 입력한 사용 횟수에서 알파벳이 나온 만큼을 제한다.
  • 앞 알파벳과 동일한 경우 -1을 원상복구 하기 위해 +1을 해준다.
  • 시의 내용을 모두 입력 가능하면 다음으로 제목이 입력 가능한지 판단해야 한다.
  • 제목은 각 단어의 앞 문자만을 대문자로 변경하여 기록하므로 먼저 제목을 추출하고 마찬가지로 알파벳 사용이 가능한지 판단한다.
  • 시의 내용과 제목 모두 기록이 가능하면 제목을 저장한 string을 그대로 출력하고 아닌 경우는 -1을 출력한다.

참고 : https://github.com/Junuux/Algorithm/tree/main/Silver/19948

 

GitHub - Junuux/Algorithm

Contribute to Junuux/Algorithm development by creating an account on GitHub.

github.com

 

'Algorithm' 카테고리의 다른 글

백준 1654번 : 랜선 자르기  (0) 2022.02.12
백준 20291번 : 파일 정리  (0) 2022.01.25
백준 17413번 : 단어 뒤집기 2  (0) 2022.01.25
백준 1972번 : 놀라운 문자열  (0) 2022.01.25
백준 1764번 : 듣보잡  (0) 2022.01.25