题目连接:UVA - 11584 Partitioning by Palindromes
#include <bits/stdc++.h>
using namespace std;
char s[1005];
int dp[1005];
int check(int l, int r){
while(l<r){
if(s[l] != s[r]) return 0;
++l; --r;
}
return 1;
}
int main()
{
std::ios::sync_with_stdio(0);
int t, n, m, i, j, k;
scanf("%d", &t);
while(t--)
{
scanf("%s", s + 1);
memset(dp, 0, sizeof(dp));
int len = strlen(s + 1);
for(i = 1; i <= len; i++)
{
dp[i] = 0x3f3f3f;
for(j = 1; j <= i; j++)
{
if(check(j, i))dp[i] = min(dp[j - 1] + 1, dp[i]);
}
}
printf("%d\n", dp[len]);
}
return 0;
}