컴퓨터공학/알고리듬 풀이

백준-재귀 25501 재귀의 귀재

cyphen156 2025. 7. 11. 11:49

재귀의 귀재

팰린드롬을 기억하는가?

https://cyphen156.tistory.com/205

 

백준-심화 1 10988 팰린드롬인지 확인하기

팰린드롬인지 확인하기팰린드롬이란 앞에서 부터 읽던지 뒤에서 부터 읽던지 똑같은 단어를 의미한다슈퍼주니어 노래중에 로꾸꺼를 한번 들어보면 확실하게 감이 온다.수박이박수, LeveL과 같

cyphen156.tistory.com

이걸 재귀로 풀면 된다.

팰린드롬이면 1 아니면 0을 출력하면 된다.

공백 이후 함수 호출 횟수를 출력한다.

제약사항

  • 0 < T <= 1,000
  • Alpha is UpperCase

주의 사항

  • 문자열 길이가 홀수인 경우를 처리해야 한다.

CPP풀이

재귀의 귀재_25501.cpp

/**
 * 백준 재귀의 귀재_25501
 * 팰린드롬을 기억하는가?
 * https://cyphen156.tistory.com/205
 * 이걸 재귀로 풀면 된다.
 * 팰린드롬이면 1 아니면 0을 출력하면 된다.
 * 공백 이후 함수 호출 횟수를 출력한다.
 * 
 * 제한사항
 *****************************************
 * 0 < T <= 1,000                        *
 * Alpha is UpperCase                    *
 *****************************************
 *
 *
 *
 * 주의
 * 없다.
 * 
 * 풀이시간 0분
 */


#include <iostream>
#include <string>

using namespace std;

int recursion(const string& str, int left, int right);
int isPalindrome(const string& str);

static int callCount = 0;

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int T;
    cin >> T;

    for (int i = 0; i < T; ++i)
    {
        string input;
        cin >> input;

        callCount = 0;

        cout << isPalindrome(input) << ' ' << callCount << '\n';
    }
    return 0;
}

int recursion(const string& str, int left, int right)
{
    callCount++;
    if (left >= right)
    {
        return 1;
    }
    else if ((str)[left] != (str)[right])
    {
        return 0;
    }
    else
    {
        return recursion(str, left + 1, right - 1);
    }
}

int isPalindrome(const string& str)
{
    return recursion(str, 0, str.length() - 1);
}

모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.

 

Workspace/알고리듬 풀이 at main · cyphen156/Workspace

Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.

github.com