1. 문제 풀이
알고리즘 유형은 구현입니다. 입력으로 주어진 문자열이 palindrome인지 판별하는 문제입니다. 이때, 문자를 하나 삭제한 경우 palindorme인 경우도 따로 판별하셔야 합니다. 여기서 WA를 받았습니다. 두 문자가 서로 다르고 아직 문자를 삭제하지 않은 경우, (st + 1, en)과 (st, en - 1) 중 하나라도 palindrome이라면 답을 1로 갱신해 출력했습니다.
2. 코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pi; typedef pair<ll, ll> pl;
typedef tuple<int, int, int> ti; typedef tuple<ll, ll, ll> tl; typedef vector<int> vi; typedef vector<ll> vl;
typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<ti> vti; typedef vector<tl> vtl;
typedef vector<string> vs; typedef vector<bool> vb; typedef queue<int> qi; typedef queue<ll> ql;
typedef queue<pi> qpi; typedef queue<pl> qpl; typedef queue<ti> qti; typedef queue<tl> qtl;
#define fastio(x, y) cin.tie((x))->sync_with_stdio((y))
#define X first
#define Y second
#define pb push_back
#define sz(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const char nl = '\n';
string str;
bool is_palindrome(int st, int en) {
while(st < en) {
if(str[st] == str[en]) {
st++; en--;
continue;
}
return 0;
}
return 1;
}
void solve(int tc) {
cin >> str;
int ans{-1};
int st{}, en{sz(str) - 1};
while(st < en) {
if(str[st] == str[en]) {
st++; en--;
continue;
}
if(ans == -1 && (is_palindrome(st + 1, en) || is_palindrome(st, en - 1))) {
ans = 1;
break;
}
ans = 2;
break;
}
cout << max(ans, 0) << nl;
}
int main() {
fastio(nullptr, false);
int tc{};
cin >> tc;
for(int i = 1; i <= tc; i++) solve(i);
}
3. 제출 결과
'알고리즘 > 백준' 카테고리의 다른 글
백준 16234번 인구 이동[C++] (0) | 2025.01.18 |
---|---|
백준 1800번 인터넷 설치[C++] (0) | 2025.01.17 |
백준 3055번 탈출[C++] (0) | 2025.01.12 |
백준 9470번 Strahler 순서[C++] (0) | 2025.01.08 |
백준 16500번 문자열판별[C++] (0) | 2025.01.07 |