17609번

seuls2·2023년 9월 1일

BOJ

목록 보기
51/55
post-thumbnail

17609

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int canPalindrome(string word, int start, int end)
{
    while (start < end)
    {
        if (word[start] == word[end])
        {
            start++;
            end--;
        }
        else
        {
            return 2;
        }
    }
    return 1;
}

int isPalindrome(string word)
{
    int start = 0;
    int end = word.length() - 1;
    while (start < end)
    {
        if (word[start] == word[end])
        {
            start++;
            end--;
        }
        else
        {
            int result1 = canPalindrome(word, start + 1, end);
            int result2 = canPalindrome(word, start, end - 1);
            if (result1 == 1 || result2 == 1)
            {
                return 1;
            }
            else
            {
                return 2;
            }
        }
    }
    return 0;
}

int main()
{
    int n;
    vector<string> words;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string word;
        cin >> word;
        words.push_back(word);
    }

    for (int i = 0; i < words.size(); i++)
    {
        cout << isPalindrome(words[i]) << endl;
    }
}
profile
공부 기록용 ( ᵕ·̮ᵕ )♩

0개의 댓글