백준 2941 python [크로아티아 알파벳]

인지용·2025년 2월 16일
0

알고리즘

목록 보기
41/46
post-thumbnail

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

import sys

# with open("./data.txt", "r") as file:
#     def input():
#         return file.readline().strip()
    
def input():
    return sys.stdin.readline().strip()
    
str = input()
arr = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z=']
cnt = 0
strLength = 0
lastIndex = 0
tempStr = ""

for i in range(len(str)):
    tempStr += str[i]
    
    for target in arr:
        if target in tempStr[lastIndex:]:
            cnt += 1
            lastIndex = i+1
            strLength += len(target)
            break

print(cnt + len(str) - strLength)

입력된 문자열 개수만큼 반복하면서 문자열을 하나씩 추가하다가
크로아티아 알파벳이 나오면 카운트를 올리고
해당 인덱스부터 다시 문자열을 비교하도록 lastIndex를 업데이트한다.

그리고 카운트 + 크로아티아 알파벳 문자열 길이 - 전체 문자열길이를 해준다면
완성
profile
한-줄

0개의 댓글