본문 바로가기
백준알고리즘/재귀

(Python/🥉2) 25501번: 재귀의 귀재

by windy7271 2022. 11. 5.
728x90
반응형

문제 출처:https://www.acmicpc.net/problem/25501

 

문제 풀이:

import sys
sys.stdin = open('/Users/song/Desktop/Python/Python/h.txt', 'r')

n = int(input())


def recursion(s, l, r):
    if l >= r: return 1,l
    elif s[l] != s[r]: return 0,l
    else: return recursion(s, l+1, r-1)
def isPalindrome(s):
    return recursion(s, 0, len(s)-1)
for i in range(n):

    isp, recu =isPalindrome(input().strip("\n"))
    print(isp, recu+1) # 0부터 했으니

 

힌트가 사실상

def recursion(s, l, r):
    if l >= r: return 1,l
    elif s[l] != s[r]: return 0,l
    else: return recursion(s, l+1, r-1)
def isPalindrome(s):
    return recursion(s, 0, len(s)-1)

이거라서 따라가면 된다.

recu +1 은 처음 ispalindrome 갈때 0 부터 들어가기때문에 +1을 해준다.

 

반응형

댓글