공부, 기록

5550. 나는 개구리로소이다 D4 파이썬 본문

코딩

5550. 나는 개구리로소이다 D4 파이썬

무는빼주세요 2020. 8. 23. 12:56

문제링크 : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWWxqfhKAWgDFAW4&categoryId=AWWxqfhKAWgDFAW4&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

def solution(croaks):
    stack=list()
    croaks=list(croaks)
    croaklist=[0]*5
    count = 0
    for i in croaks:
        if i == "c":
            stack.append(i)
            croaklist[0]=croaklist[0]+1
            count = max(count, croaklist[0])
        elif i == "r":
            if "c" in stack:
                if stack.count("c") > stack.count("r"):
                    stack.append(i)
                    croaklist[1]=croaklist[1]+1
                else:
                    return -1
            else:
                return -1
        elif i == "o":
            if "c" in stack and "r" in stack:
                if stack.count("r") > stack.count("o"):
                    stack.append(i)
                    croaklist[2]=croaklist[2]+1
                else:
                    return -1
            else:
                return -1
        elif i == "a":
            if "c" in stack and "r" in stack and "o" in stack:
                if stack.count("o") > stack.count("a"):
                    stack.append(i)
                    croaklist[3]=croaklist[3]+1
                else:
                    return -1
            else:
                return -1
        elif i == "k":
            if "c" in stack and "r" in stack and "o" in stack and "a" in stack:
                if stack.count("a") > stack.count("k"):
                    stack.append(i)
                    croaklist[4]=croaklist[4]+1
                    stack.pop(stack.index("c"))
                    stack.pop(stack.index("r"))
                    stack.pop(stack.index("o"))
                    stack.pop(stack.index("a"))
                    stack.pop(stack.index("k"))
                    for j in range(4):
                        croaklist[j]=croaklist[j]-1
                else:
                    return -1
            else:
                return -1
    if stack:
        return -1
    return count

def main():
    T = int(input())

    for test_case in range(1,T+1):
        croak = input()
        print('#{} {}'.format(test_case, solution(croak)))

main()