본문 바로가기

개발/알고리즘

백준 10988 팰린드롬인지 확인하기 C++

문제

10988번: 팰린드롬인지 확인하기 (acmicpc.net)

 

10988번: 팰린드롬인지 확인하기

첫째 줄에 단어가 주어진다. 단어의 길이는 1보다 크거나 같고, 100보다 작거나 같으며, 알파벳 소문자로만 이루어져 있다.

www.acmicpc.net

코드

#include<bits/stdc++.h>

using namespace std;

string s;

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

	cin >> s;

	int half = s.size() / 2;
	if (s.size() % 2 != 0)
	{
		s.erase(s.begin() + half, s.begin() + half + 1);
	}

	reverse(s.begin(), s.begin() + half);
	bool fit = true;
	for (int i = 0; i < half; i++)
	{
		if (s[i] == s[i + half])
			continue;
		else
		{
			fit = false;
			cout << 0 << "\n";
			break;
		}
	}
	if (fit)
	{
		cout << 1 << "\n";
	}

	return 0;
}

다른 코드

#include<bits/stdc++.h>

using namespace std;   

string s, temp; 

int main(){
    cin >> s; 
    temp = s; 
    reverse(temp.begin(), temp.end()); 
    if(temp == s) cout << 1 << "\n"; 
    else cout << 0 << "\n"; 
      
    return 0; 
}

이렇게 하면..훨씬 간단하다..............T T

 

걸린시간

50분

'개발 > 알고리즘' 카테고리의 다른 글

백준 11655번 ROT13 C++  (0) 2021.01.04
백준 1159 농구 경기 C++  (0) 2021.01.03
백준 2979 트럭 주차 C++  (0) 2021.01.03
백준 10808 알파벳 개수 C++  (0) 2021.01.03
백준 2309 일곱 난쟁이 C++  (0) 2021.01.03