main algorithm:
to count how many Palindromes a string include
give out all the way it combine, and judge whether its a Palindromes, if it is, f[i] = min(f[i]. f[j-1]+1)
code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1005;
int dp[N][N];
char str[N];
int f[N];
bool judge(int l, int r) {
while(l<r) {
if(str[l] == str[r])
l++, r--;
else return false;
}
return true;
}
int main() {
int kase;
scanf("%d", &kase);
while(kase--) {
memset(f, 0, sizeof(f));
scanf("%s", str+1);
int len = strlen(str+1);
for(int i=1; i<=len; i++) {
f[i] = i;
for(int j=1; j<=i; j++) {
if(judge(j, i))
f[i] = min(f[i], f[j-1]+1);
}
}
printf("%d\n", f[len]);
}
return 0;
}