UVA-1626 Brackets sequence (simple interval DP)

Source: Internet
Author: User

Title: It is legal to give a sequence of parentheses and brackets to satisfy the three conditions in a question. When not satisfied, it is illegal to add a minimum number of parentheses to an illegal sequence to make it legal. Outputs the shortest legal sequence.

Topic Analysis: This is an example in the "Getting Started classic". If only the shortest sequence is very simple, the definition DP (I,J) represents the minimum number of characters to be added as a valid interval i~j.

DP (I,J) =DP (i+1,j-1) (I and J can match), DP (I,J) =min (DP (I,K) +DP (k+1,j)).

However, the output time is relatively slow. From left to right, select the best scheme recursive output (see the book Code) by comparison.

The code is as follows:

# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace        Std;char p[105];int dp[105][105];const int inf=100000;bool match (int x,int y) {if (p[x]== ' (' &&p[y]== ') ')    return true;    if (p[x]== ' [' &&p[y]== '] ') return true; return false;}    void DP () {int len=strlen (p);            for (int l=1;l<=len;++l) {for (int i=0;i+l-1<len;++i) {int r=i+l-1;                if (l==1) {dp[i][r]=1;            Continue                } if (l==2) {if (Match (i,r)) dp[i][r]=0;                else dp[i][r]=2;            Continue            } Dp[i][r]=inf;            if (Match (I,R)) dp[i][r]=dp[i+1][r-1];            for (int k=i;k<r;++k) {dp[i][r]=min (dp[i][r],dp[i][k]+dp[k+1][r]);    }}}}void print (int i,int j) {if (i>j) return; if (i==j) {if (p[i]==' (' | |        p[i]== ') printf ("()");        else printf ("[]");    return;    } int ans=dp[i][j];        if (Match (I,J) &&ans==dp[i+1][j-1]) {printf ("%c", P[i]);        Print (i+1,j-1);        printf ("%c", P[j]);    return;            } for (int k=i;k<j;++k) {if (Ans==dp[i][k]+dp[k+1][j]) {print (i,k);            Print (K+1,J);        return;    }}}int Main () {int T;    scanf ("%d", &t);    GetChar ();        while (t--) {GetChar ();        Gets (p);        int Len=strlen (p);            if (len>0) {DP ();        Print (0,len-1);        } printf ("\ n");    if (T) printf ("\ n"); } return 0;}

  

UVA-1626 Brackets sequence (simple 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.