오뚝이개발자

[Google Kick Start] 2021 구글 킥스타트 Round G Dogs and Cats 풀이 본문

코딩 테스트/Google Kick Start

[Google Kick Start] 2021 구글 킥스타트 Round G Dogs and Cats 풀이

땅어 2021. 11. 18. 22:12
728x90
300x250

 

문제


 

 

나의 풀이


간단한 구현 문제이다. NO를 출력하는 경우는 다음의 두 가지이다. 

  1. S[i]=='C' and C==0 and i<dog_max (dog_max는 입력에서 D가 마지막으로 등장하는 index이다.)
  2. S[i]=='D' and D==0

위의 두 가지 경우 이외에는 YES를 출력해주면 된다. 변수 C와 D의 값은 만날 때마다 적절히 업데이트 해주면 된다.

 

코드


# https://codingcompetitions.withgoogle.com/kickstart/round/00000000004362d6/00000000008b3771

T = int(input())
for t in range(T):
    N, D, C, M = list(map(int, input().split()))
    S = input()
    dog_max = -1   # max index of 'D'
    for i in range(N-1, -1, -1):
        if S[i] =='D':
            dog_max = i
            break
    if dog_max == -1:
        print(f"Case #{t+1}: YES")
        continue
    ans = "YES"
    for i in range(N):
        # two NO case
        if S[i]=='C' and C==0 and i<dog_max:
            ans = 'NO'
            break
        elif S[i]=='D' and D==0:
            ans = 'NO'
            break
        # update
        if S[i] == 'C':
            C -= 1
        elif S[i] == 'D':
            D -= 1
            C += M

    print(f"Case #{t+1}: " + ans)

 

 

 

 

728x90
300x250
Comments