Meaning of the questions:
To a string, it is divided into a number of required sub-strings that each sub-string is a palindromic sequence. Can be split into at least ask how many.
Method a: f [i] i represents the number of strings in the string ending least divisible. f [i] = min {f [j] +1, string [j, i] palindromic sequence && 1 <= j <= i}
Method two: the definition DP [i] is the number of 1 to i before the i-th character is divided into at least palindrome string. State transition equation is:
dp (i) = min {dp (j - 1) + 1} 1 <= j <= i, if the character among the (j, i) is the palindromic string. O (n ^ 2) pre-s [i..j] whether palindromic sequence, each enumeration center, extending continuously about the mark, until a different character extending approximately up to. You can also use memory search (self-reference Liuru Jia).
Code:
// UVa11584 Partitioning by Palindromes
// Rujia Liu
// This code is slightly different from the book.
// It uses memoization to judge whether s[i..j] is a palindrome.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 5;
int n, kase, vis[maxn][maxn], p[maxn][maxn], d[maxn];
char s[maxn];
///记忆化搜索
int is_palindrome(int i, int j)
{
if(i >= j) return 1;
if(s[i] != s[j]) return 0;
if(vis[i][j] == kase) return p[i][j];
vis[i][j] = kase;
p[i][j] = is_palindrome(i+1, j-1);
return p[i][j];
}
int main()
{
int T;
scanf("%d", &T);
memset(vis, 0, sizeof(vis));
for(kase = 1; kase <= T; kase++)
{
scanf("%s", s+1);
n = strlen(s+1);
d[0] = 0;
for(int i = 1; i <= n; i++)
{
d[i] = i+1;///每个字母作为一个回文串
for(int j = 0; j < i; j++)
if(is_palindrome(j+1, i)) d[i] = min(d[i], d[j] + 1);
}
printf("%d\n", d[n]);
}
return 0;
}