BOJ
백준 8892번: 팰린드롬
혀내
2022. 1. 31. 23:22
반응형
풀이
무식하게 반복문 여러 개로 풀면 안될까봐 엄청 고민했는데, 그냥 무식하게 풀면 된다 🤯 주어진 단어들을 모두 두 개씩 뽑아 합쳐보면서 팰린드롬인지, 아닌지 확인하면 된다.
코드
#include <iostream>
#include <string>
#define MAX_K 101
using namespace std;
int T, k;
string answer;
string words[MAX_K];
void init() {
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(0);
}
int main() {
init();
cin >> T;
while (T--) {
bool find = false;
cin >> k;
for (int i = 0; i < k; i++) {
cin >> words[i];
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
if (i == j) continue;
bool isPalindrome = true;
answer = words[i] + words[j];
int length = answer.length();
for (int w = 0; w < length / 2; w++){
if (answer.at(w) != answer.at(length - 1 - w)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
find = true;
break;
}
}
if (find) break;
}
if (find) cout << answer << "\n";
else cout << "0" << "\n";
}
return 0;
}
반응형