problem uva11584-partitioning by Palindromesaccept:1326 submit:7151
Time limit:3000 MSec problem Description
Input
Input begins with the number N of test cases. Each test case consists of a single line of between 1 and lowercase letters, with no whitespace within.
Outputfor each test case, output a line containing the minimum number of groups required to partition the input into group S of Palindromes. Sample INPUT3 racecar Fastcar AAADBCCB sample Output
1
7
3
Solution: The idea is obvious, dp[i] meaning is the first character of the string can be divided into the minimum number of palindrome, the definition of this state is very simple, dp[i] must be transferred from DP[J] (j<i) and need j+1 to I is a palindrome, at this time
Dp[i] = min (Dp[i], dp[j] + 1), the equation has, the boundary is not difficult place.
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 Const intMAXN = ++Ten;6 Const intINF =0x3f3f3f3f;7 8 CharSTR[MAXN];9 Ten intIS_PALINDROMES[MAXN][MAXN]; One intDP[MAXN]; A - intIs_palindromes (intJinti) { - if(J >= i)return 1; the if(Is_palindromes[j][i]! =-1)returnIs_palindromes[j][i]; - - if(Str[i] = =Str[j]) { - returnIs_palindromes[j][i] = Is_palindromes (j +1I1); + } - Else returnIs_palindromes[j][i] =0; + } A at intMain () - { - //freopen ("Input.txt", "R", stdin); - inticase; -scanf"%d", &icase); - while(icase--) { inscanf"%s", str +1); -memset (Is_palindromes,-1,sizeof(Is_palindromes)); todp[0] =0; + intlen = strlen (str +1); - for(inti =1; I <= Len; i++) { theDp[i] =i; * for(intj =0; J < I; J + +) { $ if(Is_palindromes (j +1, i)) dp[i] = min (Dp[i], dp[j] +1);Panax Notoginseng } - } theprintf"%d\n", Dp[len]); + } A return 0; the}
Uva11584-partitioning by palindromes (Dynamic planning Basics)