From 104fe7810fa395c21322f4900aba7d47ae31add4 Mon Sep 17 00:00:00 2001 From: kimhojun <63347089+esc10946@users.noreply.github.com> Date: Fri, 9 Jan 2026 09:15:22 +0900 Subject: [PATCH] =?UTF-8?q?260109=20:=20[BOJ=201891]=20=EC=82=AC=EB=B6=84?= =?UTF-8?q?=EB=A9=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _hojun/1891.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 _hojun/1891.cpp diff --git a/_hojun/1891.cpp b/_hojun/1891.cpp new file mode 100644 index 00000000..16999b16 --- /dev/null +++ b/_hojun/1891.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; +int n; +int arr[5001]; +int dp[5001][5001]; + +int pal(int s, int e) { + if (s >= e) return 0; // 종료조건 + + if (dp[s][e] != -1) return dp[s][e]; + + int cnt = 0; + if (arr[s] == arr[e]) { + cnt = pal(s + 1, e - 1); + } + + else { + // s앞에 arr[e] 값 추가, e뒤에 arr[s] 값 추가 + cnt = min(pal(s, e - 1) + 1, pal(s + 1, e) + 1); + } + + return dp[s][e] = cnt; +} + + +int main() { + cin >> n; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + dp[i][j] = -1; + } + } + + cout << pal(0, n - 1); + + return 0; +}