[Uva-11584] partitioning by palindromes (DP)

Source: Internet
Author: User

The rough complexity is L ^ 3, and the maximum length is 1000. I did not dare to do it. Later, I found that the complexity coefficient is not big, and it can pass through very quickly.

DP [J] = DP [I-1] + 1 (if (STR [I] ~ STR [J] is a reply)

14327451 11584 Partitioning by palindromes Accepted C ++ 0.052 09:33:17
#include<set>#include<map>#include<stack>#include<queue>#include<vector>#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<cstring>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;const int maxn = 1111;char str[maxn];int   dp[maxn];bool is_part(int i,int j){    while(i < j){        if(str[i] != str[j])            return false;        i ++;        j --;    }    return true;}int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%s",str);        int L = strlen(str);        for(int i = 0 ; i <= L; i ++) dp[i] = i + 1;        for(int j = 0 ; j < L; j ++) // L            for(int i = 0 ; i <= j ; i ++){ // L                if(is_part(i,j)){ // L                    if(i - 1 >= 0)                        dp[j] = min(dp[j],dp[i - 1] + 1);                    else                        dp[j] = 1;                }            }        printf("%d\n",dp[L - 1]);    }    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.