팰린드롬 시리즈

Jin Hur·2021년 10월 16일

알고리즘(Algorithm)

목록 보기
46/49

팰린드롬 - 10174번 백준 : 팰린드롬인지 아닌지 확인


시간복잡도: O(N/2)
차례대로 앞부분 문자와 뒷 부분 문자를 비교한다.

입력 주의!
cin을 쓰다가 scanf()나 getline() 함수를 사용하려면 사용전 cin 버퍼를 비워주자. 개행문자를 뱉어내야 한다.

#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin >> t;
    // 이 후 scanf(또는 getline) 함수를 위해 버퍼안 개행문자를 뱉어준다./////
    cin.ignore();
    //////////////////////////////////////////////////////////////////////
    vector<string> answers;

    for(int i=1; i<=t; i++){

        vector<char> str;
        while(true){
            char x;
            scanf("%1c", &x);

            if(x == '\n')
                break;
            
            // 대문자 처리
            if(x >= 'A' && x <= 'Z'){
                x = x - ('A' - 'a');
            }

            str.push_back(x);
        }


        bool flag = false;
        for(int j=0; j<str.size()/2; j++){
            if(str[j] != str[str.size()-1-j]){
                flag = false;
                break;
            }
            else{
                flag = true;
            }
        }
        if(flag)
            answers.push_back("Yes");
        else
            answers.push_back("No");
    }

    for(int i=0; i<answers.size(); i++){
        cout << answers[i] << '\n';
    }
}

0개의 댓글