#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, -1sizeof(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

+ Recent posts