UVA 11584 Partitioning by palindromes (DP)

Source: Internet
Author: User

Topic links

Test instructions: Given a string, decomposed into multiple substrings, each substring is a palindrome string, ask at least how many substrings can be divided.

Exercises

Dp[i] means that the first I string is divided into a minimum number of palindrome substrings;

0<=j<=i; if the string from J to I is a palindrome, then Dp[i]=min (dp[i],dp[j-1]+1);

#include <iostream>using namespace Std;int dp[1005];string s;bool ok (int j,int i) {while    (j<=i)    {        if (S[j]!=s[i]) return false;        i--;        j + +;    }    return true;} int main () {    int n;    cin>>n;    while (n--)    {        cin>>s;        int len=s.length ();        for (int i=0;i<len;i++)            dp[i]=i+1;        for (int i=1;i<len;i++) for            (int j=0;j<=i;j++)            {                if (ok (j,i))                {                    if (j==0) dp[i]=1;                    else Dp[i]=min (Dp[j-1]+1,dp[i]);                }            }        cout<<dp[len-1]<<endl;    }    return 0;}

  

UVA 11584 Partitioning by palindromes (DP)

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.