백준 25501번 재귀의 귀재

Montag·2023년 1월 31일
1

문제풀이

목록 보기
4/10

25501번 재귀의 귀재

문제
정휘는 후배들이 재귀 함수를 잘 다루는 재귀의 귀재인지 알아보기 위해 재귀 함수와 관련된 문제를 출제하기로 했다.


팰린드롬이란, 앞에서부터 읽었을 때와 뒤에서부터 읽었을 때가 같은 문자열을 말한다. 팰린드롬의 예시로 AAA, ABBA, ABABA 등이 있고, 팰린드롬이 아닌 문자열의 예시로 ABCA, PALINDROME 등이 있다.


어떤 문자열이 팰린드롬인지 판별하는 문제는 재귀 함수를 이용해 쉽게 해결할 수 있다. 아래 코드의 isPalindrome 함수는 주어진 문자열이 팰린드롬이면 1, 팰린드롬이 아니면 0을 반환하는 함수다.

#include <stdio.h>
#include <string.h>

int recursion(const char *s, int l, int r){
    if(l >= r) return 1;
    else if(s[l] != s[r]) return 0;
    else return recursion(s, l+1, r-1);
}

int isPalindrome(const char *s){
    return recursion(s, 0, strlen(s)-1);
}

int main(){
    printf("ABBA: %d\n", isPalindrome("ABBA")); // 1
    printf("ABC: %d\n", isPalindrome("ABC"));   // 0
}

정휘는 위에 작성된 isPalindrome 함수를 이용하여 어떤 문자열이 팰린드롬인지 여부를 판단하려고 한다.


구체적으로는, 문자열
SS를 isPalindrome 함수의 인자로 전달하여 팰린드롬 여부를 반환값으로 알아낼 것이다. 더불어 판별하는 과정에서 recursion 함수를 몇 번 호출하는지 셀 것이다.


정휘를 따라 여러분도 함수의 반환값과 recursion 함수의 호출 횟수를 구해보자.

입력
첫째 줄에 테스트케이스의 개수
TT가 주어진다. (
1T10001 \leq T \leq 1\,000)


둘째 줄부터
TT개의 줄에 알파벳 대문자로 구성된 문자열
SS가 주어진다. (
1S10001 \leq \vert S\vert \leq 1\,000)

출력
각 테스트케이스마다, isPalindrome 함수의 반환값과 recursion 함수의 호출 횟수를 한 줄에 공백으로 구분하여 출력한다.

예제 입력 1
5
AAA
ABBA
ABABA
ABCA
PALINDROME

예제 출력 1
1 2
1 3
1 3
0 2
0 1

힌트
Python 3

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

def isPalindrome(s):
    return recursion(s, 0, len(s)-1)

print('ABBA:', isPalindrome('ABBA'))
print('ABC:', isPalindrome('ABC'))

Java

public class Main{
    public static int recursion(String s, int l, int r){
        if(l >= r) return 1;
        else if(s.charAt(l) != s.charAt(r)) return 0;
        else return recursion(s, l+1, r-1);
    }
    public static int isPalindrome(String s){
        return recursion(s, 0, s.length()-1);
    }
    public static void main(String[] args){
        System.out.println("ABBA: " + isPalindrome("ABBA"));
        System.out.println("ABC: " + isPalindrome("ABC"));
    }
}

나의 풀이

이 문제는 풀이 함수가 주어졌기 때문에,
펠린드롬 함수가 몇 번 호출되었는지만 처리해주면 됐다

전역변수 처리하여
recursion 메서드가 호출될 때 마다 ++해줬다

처음에는 cnt가 0으로 초기화되지 않아 계속 수가 증가하여,
main 함수 안에서 반복문 시작과 함께 0으로 초기화 하였다

import java.util.Scanner;

public class _25501_재귀의귀재{
	static int cnt = 0;
	
    public static int recursion(String s, int l, int r){
    	cnt++;
        if(l >= r) return 1;
        else if(s.charAt(l) != s.charAt(r)) return 0;
        else return recursion(s, l+1, r-1);
        
    }
    
    public static int isPalindrome(String s){
        return recursion(s, 0, s.length()-1);
    }
    
    public static void main(String[] args){
    	
    	Scanner sc = new Scanner(System.in);
    	
    	int T = sc.nextInt();
    	
    	for(int tc=0; tc<T; tc++) {
    		cnt = 0;
    		System.out.printf("%d %d",isPalindrome(sc.next()), cnt);
    		System.out.println();
    	}
    	        
    }
}
profile
안녕하세요

2개의 댓글

comment-user-thumbnail
2023년 1월 31일

감사합니다 큰 도움이 되었어요 ㅠㅠ

답글 달기
comment-user-thumbnail
2023년 1월 31일

-싸피의 귀재-

답글 달기