Test instructions: There is a string that asks how many palindrome strings can be cut into as few as possible.
The F[i] indicates how many times the string of the former I has as few palindrome strings as possible, using an array of flag[i][j] to indicate whether the string from position I to position J is a palindrome string, if so, f[i] = min{f[i-1] + 1, f[j-1] + 1}.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;const int N = 2005;const int INF = 0x3f3f3f3f;char str[n];int f[n], flag[n][n];bool judge (int l, int r) {for (int i = l, j = r; I <= R; i++, j-- if (str[i]! = Str[j]) return False;return true;} int main () {int t;scanf ("%d", &t), while (t--) {scanf ('%s ', str + 1), int len = strlen (str + 1); for (int i = 1; I <= Len i++) for (int j = i; J <= Len; j + +) Flag[i][j] = Judge (I, J); Memset (f, INF, sizeof (f)); F[0] = 0;for (int i = 1; I <= l En i++) {F[i] = F[i-1] + 1;for (int j = i-1; J >= 1; j--) if (flag[j][i] = = 1) f[i] = min (F[i], f[j-1] + 1);} printf ("%d\n", F[len]);} return 0;}
UVA 11584 (DP)