Lightoj-1044-palindrome partitioning (interval DP)

Source: Internet
Author: User

1044-palindrome Partitioning
PDF (中文版) statisticsforum
Time Limit:1 second (s) Memory limit:32 MB
A palindrome partition is the partitioning of a string such, each separate substring is a palindrome.

For example, the string "Abacaba" could is partitioned in several different ways, such as {"A", "B", "a", "C", "a", "B", "a"}, { "A", "Bacab", "a"}, {"ABA", "C", "ABA"}, or {"Abacaba"}, among others.

You is given a string s. Return the minimum possible number of substrings in a palindrome partition of S.

Input
Input starts with an integer T (≤40), denoting the number of test cases.

Each case begins with a non-empty string s of uppercase letters with length no more than 1000.

Output
For each case of input, the case number and the desired result has been to print.

Sample Input
Output for Sample Input
3
Aaaa
Abcdefgh
Qwertytrewqwert
Case 1:1
Case 2:8
Case 3:5

Problem solving: Preprocess a palindrome string and then Dfs.

But dp[l][r] = min (Dp[l][r],dfs (l,i) +dfs (i+1,r)); This kind of DFS will explode

Careful analysis will find (Palindrome string + a string of letters) + a string of letters and palindrome string + (a string of letters + a string of letters) is the same situation, so may omit the previous DFS (l,i) situation

Direct if (Judge[l][i]) dp[l][r] = min (Dp[l][r],1+dfs (i+1,r));

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespacestd;Const intINF =1e9;intT,len;Charstr[1100];intdp[1100][1100];BOOLjudge[1100][1100];voidinit () {memset (judge,false,sizeof(judge)); Memset (DP,-1,sizeof(DP));  for(intI=0; i<len;i++){         for(intj=i;j<len;j++){            intFlag =0;  for(intz=i;z<= (I+J)/2; z++){                if(Z-str[z]!=str[j-i)]) {Flag=1; Break; }            }            if(Flag = =0) Judge[i][j] =true; }    }    }intDfsintLintR) {        if(Judge[l][r]) {Dp[l][r]=1; return 1; }    if(dp[l][r]!=-1)returnDp[l][r]; DP[L][R]=inf;  for(inti=l;i<r;i++){                if(Judge[l][i]) Dp[l][r]= Min (Dp[l][r],1+dfs (i+1, R)); //Dp[l][r] = min (Dp[l][r],dfs (l,i) +dfs (i+1,r));     }    returndp[l][r];}intMain () {scanf ("%d",&T);  for(intt=1; t<=t;t++) {scanf ("%s", str); Len=strlen (str);        Init (); printf ("Case %d:%d\n", T,dfs (0, len-1)); }        return 0;}
View Code

Lightoj-1044-palindrome partitioning (interval 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.