Uva11584-partitioning by palindromes (Dynamic planning Basics)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.