
안녕하세요 !
오늘은 백준 25501번 - 재귀의 귀재 문제를 가져왔습니다.


문제를 해결하는 방법은 다음과 같습니다.
- 입력을 받는다.
- recursion 함수에 재귀함수 호출 수를 카운트 할 수 있는 cnt인수를 만들고 반환하는 값과 cnt 인수를 호출하는 부분을 조건에 맞게 수정한다.
- isPalindrome 함수 cnt인수의 값을 1로 설정해준다.
- 입력받은 문자열을 넘긴다.
n = int(input()) arr = [] for i in range(n): arr.append(sys.stdin.readline().strip())
recursion을 재귀로 호출하는 부분에 cnt+1로 수정하여 호출될 때마다 카운트가 증가할 수 있게 수정했습니다. 그리고 return 값을 문제 결과에 맞게 문자열로 수정했습니다.
def recursion(s, l, r, cnt): if l >= r: return f"1 {cnt}" elif s[l] != s[r]: return f"0 {cnt}" else: return recursion(s, l+1, r-1, cnt+1)
def isPalindrome(s): return recursion(s, 0, len(s)-1, 1)
for s in arr: print(isPalindrome(s))
import sys
def recursion(s, l, r, cnt):
if l >= r:
return f"1 {cnt}"
elif s[l] != s[r]:
return f"0 {cnt}"
else:
return recursion(s, l+1, r-1, cnt+1)
def isPalindrome(s):
return recursion(s, 0, len(s)-1, 1)
n = int(input())
arr = []
for i in range(n):
arr.append(sys.stdin.readline().strip())
for s in arr:
print(isPalindrome(s))