i 인덱스를 word 길이까지 돌리시면 i+1이나 i+2를 참조하실 때 문제가 생깁니다.
2941번 - 크로아티아 알파벳
말씀해주신 조언 바탕으로
경우를 word 의 길이가 2 이하인 경우와 초과인 경우로 나누고
길이가 2 이하인 경우에 범위를 len(word) -1 로 설정했는데요,
그래도 런타임 에러가 나네욤... 반례가 있을까요 ㅠㅠ
import sys def croatian_alphabet_word_size_small(word): count = 0 end_range = len(word)-1 for i in range(0, end_range): if 'c' == word[i]: if '=' == word[i+1] or '-' == word[i+1]: count += 1 break else: count += 2 break elif 'd' == word[i]: if 'z' == word[i+1]: count += 2 break elif '-' == word[i+1]: count += 1 break elif 'l' == word[i] and 'j' == word[i+1]: count += 1 break elif 'n' == word[i] and 'j' == word[i+1]: count += 1 break elif 's' == word[i] and '=' == word[i+1]: count += 1 break elif 'z' == word[i] and '=' == word[i+1]: count += 1 break else: if 'c' == word[i-1] or 'd' == word[i-1] or 'l' == word[i-1] or 'n' == word[i-1] or 's' == word[i-1] or 'z' == word[i-1]: continue else: if word[i] != '=' or word[i] != '-': count += 1 else: continue return count def croatian_alphabet_word_size_large(word): count = 0 end_range = len(word) for i in range(0, end_range): if 'c' == word[i]: if '=' == word[i+1] or '-' == word[i+1]: count += 1 else: count += 1 elif 'd' == word[i]: if 'z' == word[i+1] and '=' == word[i+2]: count += 1 elif '-' == word[i+1]: count += 1 elif 'l' == word[i] and 'j' == word[i+1]: count += 1 elif 'n' == word[i] and 'j' == word[i+1]: count += 1 elif 's' == word[i] and '=' == word[i+1]: count += 1 elif 'z' == word[i] and '=' == word[i+1]: if 'd' == word[i-1]: continue else: count += 1 else: if 'c' == word[i-1] or 'd' == word[i-1] or 'l' == word[i-1] or 'n' == word[i-1] or 's' == word[i-1] or 'z' == word[i-1]: continue else: if word[i] != '=' or word[i] != '-': count += 1 else: continue return count if __name__ == '__main__': word = sys.stdin.readline() word = word.replace('\n', '') word_len = len(word) if word_len <= 2: real_char_count = croatian_alphabet_word_size_small(word) else: real_char_count = croatian_alphabet_word_size_large(word) print(real_char_count)
https://www.acmicpc.net/source/17453865
저도 푸신 방법과 직접 검사하는 방법으로 짰었는데,
효율면에서는 현재 문제에서 크게 차이나진 않지만 replace보다 짜신 방법이 더 나을거에요.
댓글을 작성하려면 로그인해야 합니다.
ssk04040 3년 전
계속 런타임 에러가 뜹니다. ㅠㅠ 무엇이 문제일까요?