http://uva.onlinejudge.org/external/115/11584.html
将所给的字符串,最少可以分成多少个回文串,dp[i]表示0~i之间最少可以分成的值,然后判断j~i(j <= i)之间是否是回文,如果是回文dp[i] = min(dp[i], dp[j - 1] + 1)
/*************************************************************************
> File Name: UVa11584.cpp
> Author: AcToy
> Mail: ycsgldy@163.com
> Created Time: 2013年07月18日 星期四 10时02分17秒
************************************************************************/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef unsigned int u32;
typedef long long i64;
typedef unsigned long long u64;
typedef vector<int> IV;
typedef vector<bool> BV;
typedef pair<int,int> II;
typedef vector<II> IIV;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
const int INF = 0x7FFFFFFF;
const double eps = 1E-10;
const double PI = acos(-1);
int Case, dp[1010];
string str;
bool is_pal(int i, int j) {
int mid = (j - i) / 2;
for(int k = 0; k <= mid; ++k)
if(str[i + k] != str[j - k]) return 0;
return 1;
}
int main()
{
cin >> Case;
while(Case--) {
cin >> str;
int len = str.length();
dp[0] = 0;
for(int i = 1; i <= len; ++i) {
dp[i] = INF;
for(int j = 1; j <= i; ++j) {
if(is_pal(j - 1, i - 1)) {
dp[i] = min(dp[i], dp[j - 1] + 1);
}
}
}
printf("%d\n", dp[len]);
}
return 0;
}