문제 링크입니다: https://www.acmicpc.net/problem/25501
25501번: 재귀의 귀재
각 테스트케이스마다, isPalindrome 함수의 반환값과 recursion 함수의 호출 횟수를 한 줄에 공백으로 구분하여 출력한다.
www.acmicpc.net
문제에서 주어진대로 재귀 호출을 할 경우 시간 초과가 발생합니다.
따라서, for문을 통해 풀어야합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int T; | |
cin >> T; | |
for (int t = 0; t < T; t++) | |
{ | |
string s; | |
cin >> s; | |
int cnt = 0; | |
int left = 0; | |
int right = s.length() - 1; | |
bool flag = true; | |
for (; left <= right; left++, right--) | |
{ | |
cnt++; | |
if (s[left] != s[right]) | |
{ | |
flag = false; | |
break; | |
} | |
} | |
cout << (flag ? 1 : 0) << " " << (flag && s.length() % 2 == 0 ? cnt + 1 : cnt) << "\n"; | |
} | |
return 0; | |
} |

개발환경:Visual Studio 2022
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 25513번 빠른 오름차순 숫자 탐색 (0) | 2022.09.04 |
---|---|
백준 25527번 Counting Peaks of Infection (0) | 2022.08.31 |
백준 15235번 Olympiad Pizza (0) | 2022.08.04 |
백준 9517번 아이 러브 크로아티아 (0) | 2021.08.19 |
백준 4562번 No Brainer (1) | 2021.07.22 |