나의 개발일지

[백준] 12919 A와 B 2 [Python, 파이썬] 본문

백준

[백준] 12919 A와 B 2 [Python, 파이썬]

YoonJuHan 2023. 10. 3. 15:22
  • 문제 : https://www.acmicpc.net/problem/12919
  • dfs
  • 🔑 마지막이 A일 때, 처음이 B일 때 
    • t[-1]이 "A"일 때 마지막 "A"빼고 재귀 호출
    • t[0]이 "B"일 때 뒤집은 후 마지막 "B"빼고 재귀 호출
    • 각각 if 조건으로 두 가지 분기로 나눈다.

 

s = input()
t = input()


def dfs(t):
    if len(s) == len(t):
        if s == t:
            print(1)
            exit()
        else:
            return 0

    if t[-1] == "A":
        dfs(t[:-1])
    
    if t[0] == "B":
        dfs(t[::-1][:-1])


dfs(t)
print(0)
Comments