#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
string word;
int dp[2510][2510];
int minResult[2510];
int solve(int s, int e)
{
if(s >= e)
{
return 1;
}
if(dp[s][e] != -1)
{
return dp[s][e];
}
dp[s][e] = 0;
if(word[s] == word[e])
{
dp[s][e] = solve(s+1, e-1);
}
return dp[s][e];
}
int main(void)
{
// freopen("B1509_input.txt", "r", stdin);
cin >> word;
word = "0" + word;
memset(dp, -1, sizeof(dp));
minResult[0] = 0;
for(int i = 1; i < word.size(); i++)
{
minResult[i] = 99999999;
for(int j = 1; j <= i; j++)
{
if(solve(j, i) == 1)
{
minResult[i] = min(minResult[i], minResult[j-1] + 1);
}
}
}
cout << minResult[word.size()-1];
return 0;
}
|
cs |
'Baekjoon > DP' 카테고리의 다른 글
[백준 2157] 여행 (DP) (C/C++) (★★★) (0) | 2021.01.26 |
---|---|
[백준 2167] 2차원 배열의 합 (DP) (C/C++) (★★) (0) | 2020.05.28 |
[백준 10942] 팰린드롬 ? (DP) (C/C++) (★★) (0) | 2020.03.23 |
[백준 11048] 이동하기 (DFS, DP) (C/C++) (★) (0) | 2020.03.23 |
[백준 1890] 점프 (DFS, DP) (C/C++) (0) | 2020.03.22 |