※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 27627번 문제인 Splitology이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/27627 

 

27627번: Splitology

If S cannot be split into two palindrome strings, then output “NO” in a line. Otherwise, output two palindrome strings A and B in a line separated by a single space such that S = A + B. If there is more than one way to split S into two palindrome strin

www.acmicpc.net

먼저, 주어지는 문자열의 길이가 2 이상 500 이하임을 확인하자. 즉, 주어지는 문자열은 항상 둘로 쪼갤 수 있는 방법이 존재하고, 그 가짓수가 500 미만임을 확인하자.

 

가짓수가 충분히 적고 문자열의 길이 또한 짧으므로, 두 문자열을 쪼개는 가능한 모든 방법에 대해 각 쪼개진 문자열이 팰린드롬인지를 확인하는 코드를 작성하고 문제를 해결하자.

 

substr을 이용하면 편하게 부분문자열을 얻을 수 있다. 또한, reverse를 이용하면 문자열을 편하게 뒤집을 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string s;
int slen;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> s;
	slen = s.length();
	for (int i = 1; i < slen; i++) {
		string s1 = s.substr(0, i), s2 = s.substr(i, slen - i);
		string ss1 = s1, ss2 = s2;
		reverse(ss1.begin(), ss1.end());
		reverse(ss2.begin(), ss2.end());

		if (s1 == ss1 && s2 == ss2) {
			cout << s1 << ' ' << s2;
			return 0;
		}
	}

	cout << "NO";
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 1983 // C++] 숫자 박스  (0) 2023.03.05
[BOJ 27621 // C++] Sum of Three Cubes  (0) 2023.03.04
[BOJ 27708 // C++] Antisort  (0) 2023.03.04
[BOJ 20877 // C++] Minigolf  (0) 2023.03.04
[BOJ 16485 // C++] 작도하자! - ②  (0) 2023.03.03

+ Recent posts