본문 바로가기
백준일지

[백준] 17609번 회문

by 민지기il 2025. 4. 13.
#include <bits/stdc++.h>
#define fastio cin.tie(0)->sync_with_stdio(0)
using namespace std;

int T;
vector<string> V;
void Input(){
    cin>>T;
    for(int i=0;i<T;i++){
        string S; cin>>S;
        V.push_back(S);
    }
}

int palindrome(int left, int right, int deleted, string str){
    while(left<right){
        if(str[left]!=str[right]){
            if(deleted==0){
                if(palindrome(left+1, right, 1,str)==0 || palindrome(left, right-1, 1, str)==0)
                    return 1;
                return 2;
            }
            else return 2;
        }
        else{
            left++;
            right--;
        }
    }
    return 0;
}
int main(){
    fastio;
    Input();
    for(int t=0; t<T; t++){
        string str=V[t];
        int result = palindrome(0, str.length()-1, 0, str);
        cout<<result<<'\n';
    }
    return 0;
}

[참고]

https://yabmoons.tistory.com/704

 

 

 

'백준일지' 카테고리의 다른 글

[백준] 2156번 포도주  (0) 2025.04.16
[프로그래머스] 후보키  (0) 2025.04.14
[백준] 1738번 병든 나이트  (0) 2025.04.12
[프로그래머스] 순위 검색  (1) 2025.04.12
[백준] 2437번 저울  (0) 2025.04.10