알고리즘/백준

[백준] 2941번: 크로아티아 알파벳 (파이썬)

김성훈0822 2023. 11. 29. 23:06

1. 문제

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

 

2941번: 크로아티아 알파벳

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=

www.acmicpc.net

 

2. 풀이

1차 시도: 실패

word = input()
count = []
i = 0

while i < len(word):
  if word[i] == 'c':
    if word[i+1] == '=':
      count.append('c=')
      i += 2
    elif word[i+1] == '-':
      count.append('c-')
      i += 2
  elif word[i] == 'd':
    if word[i+1] == 'z':
      if word[i+2] == '=':
        count.append('dz=')
        i += 3
    elif word[i+1] == '-':
      count.append('d-')
      i += 2
  elif word[i] == 'l':
    if word[i+1] == 'j':
      count.append('lj')
      i += 2
  elif word[i] == 'n':
    if word[i+1] == 'j':
      count.append('nj')
      i += 2
  elif word[i] == 's':
    if word[i+1] == '=':
      count.append('s=')
      i += 2
  elif word[i] == 'z':
    if word[i+1] == '=':
      count.append('z=')
      i += 2
  else:
    count.append(word[i])
    i += 1

print(count)

 

2차 시도: 성공

word = input()
count = 0

while word.find('c=') != -1:
  if 'c=' in word:
    word = word.replace('c=', '.', 1)
    count += 1
while word.find('c-') != -1:
  if 'c-' in word:
    word = word.replace('c-', '.', 1)
    count += 1
while word.find('dz=') != -1:
  if 'dz=' in word:
    word = word.replace('dz=', '.', 1)
    count += 1
while word.find('d-') != -1:
  if 'd-' in word:
    word = word.replace('d-', '.', 1)
    count += 1
while word.find('lj') != -1:
  if 'lj' in word:
    word = word.replace('lj', '.', 1)
    count += 1
while word.find('nj') != -1:    
  if 'nj' in word:
    word = word.replace('nj', '.', 1)
    count += 1
while word.find('s=') != -1:    
  if 's=' in word:
    word = word.replace('s=', '.', 1)
    count += 1
while word.find('z=') != -1:    
  if 'z=' in word:
    word = word.replace('z=', '.', 1)
    count += 1

word = word.replace('.', '')
count += len(word)

print(count)