본문 바로가기
알고리즘

[BOJ / 12933 / Python] 오리

by 치우치지않는 2023. 9. 22.

[ 문제 ]

https://www.acmicpc.net/problem/12933

[ 난이도 ]

실버3

[ 필요 개념 ]

그리디.......라고 하는데 사실 몰라도 괜찮을 듯

[ 풀이 ]

솔직히 힌트가 없었다면 못 풀었을 것 같.....다. 모든 글자가 다 쓰여야 한다는 사실도 처음엔 몰랐따. 뭔가 노이즈가 껴있어도 된다고 넘겨짚은 거 같은데, 그렇게 문제를 잘못 이해하는 순간 문제 해결은 산으로 간다는 것을 느꼈다. 

그리고 이번엔 처음으로.. 예외 케이스를 생각하지 못해서 중간에 틀렸습니다! 가 나왔는데.. 어디서 틀렸다는건지 모르겠어서 한참을 헤매었다. 그럴 때일수록, edge 케이스를 생각해보는 것이 상황을 진전시키는 데 도움이 되는 것 같다. 

[ 소스코드 ]

recordedDuckVoice = input()
ducks = ['']
noMatchedLetterError = False
isErrorOccured = False

for i in range (len(recordedDuckVoice)) :
  if recordedDuckVoice[i] == 'q' :
    noMatchedLetterError = True
    for index, duck in enumerate(ducks):
      if len(duck) == 0 :
        duck = duck + 'q'
        # print('duck: ' + duck)
        ducks[index] = duck
        noMatchedLetterError = False
        break
    if noMatchedLetterError == True :
      ducks.append('q')
  if recordedDuckVoice[i] == 'u' :
    noMatchedLetterError = True
    for index, duck in enumerate(ducks):
      if len(duck) == 1 :
        duck = duck + 'u'
        # print('duck: ' + duck)
        ducks[index] = duck
        noMatchedLetterError = False
        break
    if noMatchedLetterError == True :
      # print('error....1')
      isErrorOccured = True
  if recordedDuckVoice[i] == 'a' :
    noMatchedLetterError = True
    for index, duck in enumerate(ducks):
      if len(duck) == 2 :
        duck = duck + 'a'
        # print('duck: ' + duck)
        ducks[index] = duck
        noMatchedLetterError = False
        break
    if noMatchedLetterError == True :
      # print('error....2')
      isErrorOccured = True
  if recordedDuckVoice[i] == 'c' :
    noMatchedLetterError = True
    for index, duck in enumerate(ducks):
      if len(duck) == 3 :
        duck = duck + 'c'
        # print('duck: ' + duck)
        ducks[index] = duck
        noMatchedLetterError = False
        break
    if noMatchedLetterError == True :
      # print('error....3')
      isErrorOccured = True
  if recordedDuckVoice[i] == 'k' :
    noMatchedLetterError = True
    for index, duck in enumerate(ducks):
      if len(duck) == 4 :
        duck = duck + 'k'
        # print('duck: ' + duck)
        ducks[index] = ''
        noMatchedLetterError = False
        break
    if noMatchedLetterError == True :
      # print('error....4')
      isErrorOccured = True
  # print(ducks)

for duck in ducks :
  if len(duck) > 0 :
    isErrorOccured = True

# print(ducks)
if isErrorOccured :
  print(-1)
else :
  print(len(ducks))


# 제일 가까운 애한테 주도권을 주자. !!!
# q = 1
# u = 2
# a = 3
# c = 4
# k = 5
# (숫자는 문자열 길이)

# q가 나왔다면,, 정상적인 상황이라면 문자열 길이가 0인 곳이 있어야 함. 만약 없다면 새로운 duck 추가. (에러 아님)
# c가 나왔다면,, 정상적인 상황이라면 문자열 길이가 3인 곳이 있어야 함. 만약 없다면 에러. 

# quqacukqauackck
# qu_ac_kq_uack__
# __q__u__a____ck

반복되는 코드가 많다.반복문을 사용하면 좀 더 깔끔하게 정리될 것 같다. 다음부턴 반복문을 적극 활용해 보자. 

댓글