Test instructions: Given a string, it can be divided into several palindrome strings at least.
Analysis: Dp[i] means that the first I characters can be divided into several palindrome, dp[i] = min{1 + dp[j-1] | J-i is a palindrome}.
The code is as follows:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <unordered_map> #include <unordered_set> #define DEBUG () puts ("+ + + + "); #define FREOPENR freopen (" In.txt "," R ", stdin) #define FREOPENW freopen (" OUT.txt "," w ", stdout) using namespace std; typedef long Long Ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;const Dou ble PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 1e3 + 5;const int mod = 2000;const int dr[] = {-1, 1, 0, 0};c onst int dc[] = {0, 0, 1, -1};const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};iNT N, m;const int mon[] = {0, 31, 29, 31, 30, 31, 3, +, 0,,, +, +, +,. 0, from, to, 31};inline bool is_in (int r, int c) {return R >= 0 && r < n && C >= 0 & amp;& c < m;} Char s[maxn];int dp[maxn];bool judge (int i, int j) {while (I < j) {if (s[i]! = S[j]) return false; ++i,--j; } return true; int main () {int T; Cin >> T; while (t--) {scanf ("%s", s); n = strlen (s); Memset (DP, INF, sizeof DP); Dp[0] = 1; for (int i = 1; i < n; ++i) {for (int j = 0; J < i; ++j) if (judge (J, i) dp[i] = min (dp[i), 1 + dp[j-1]); else Dp[i] = min (dp[i], dp[i-1]+1); } printf ("%d\n", dp[n-1]); } return 0;}
UVa 11584 Partitioning by palindromes (simple DP)