Click Open Link
Question:
To a string, you must split it into several substrings so that each substring is a return string. The minimum number of splits.
Analysis:
F [I] indicates the minimum number of strings ending with I that can be divided.
F [I] = min {f [J] + 1, string [J, I] is a return string & 1 <= j <= I}
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> using namespace STD; typedef long int64; const int INF = 0x3f3f3f; const int maxn = 1010; char STR [maxn]; int f [maxn]; bool ispalind (int l, int R) {While (L <R) {If (STR [l]! = STR [R]) return false; ++ L; -- r;} return true;} int main () {int t; scanf ("% d", & T ); while (t --) {scanf ("% s", STR + 1); int Len = strlen (STR + 1); memset (F, 0, sizeof (f )); for (INT I = 1; I <= Len; ++ I) {f [I] = I + 1; for (Int J = 1; j <= I; + + J) if (ispalind (J, I) {f [I] = min (F [I], F [J-1] + 1 );}} printf ("% d \ n", F [Len]);} return 0 ;}