Ural 1183 Brackets Sequence (interval dp+ memory search)

Source: Internet
Author: User

Title Address: Ural 1183

Finally put this question to a.

。 Dragged on for a long time, ...

I think or can't think of it, just purple book has this problem.

D[I][J] for the input sequence from subscript I to subscript j minimum need to add how many parentheses the ability to become a legal sequence. 0<=i<=j<len (len is the length of the input sequence).


C[i][j] is the break position of the input sequence from subscript I to subscript J. Assume that there is no break-1.

When I==j. D[I][J] is 1

when s[i]== ' (' && s[j]== ') ' or s[i]== ' [' && s[j]== '] ', d[i][j]=d[i+1][j-1]

otherwise d[i ][J]=MIN{D[I][K]+D[K+1][J]} i<=k<j,  c[i][j] records the position of the fracture K

uses recursion to output print when the output of the d[i][j]

is calculated recursively (0, Len-1) The


output function is defined as print (int i, int j), indicating that the output is from subscript I to the valid sequence of subscript J


when i>j. Returns directly without the need to output


when i==j. D[I][J] is 1, at least one parenthesis is added. Suppose S[i] is ' (' or ') '. Output "()", otherwise output "[]"


when i>j, assume c[i][j]>=0. The instructions are disconnected from I to J. The print (I, c[i][j]) is called recursively, and print (c[i][j]+1, j);


                assumes c[i][j]<0, stating that there is no disconnection. Suppose s[i]== ' (' Then output ' (', print (i+1, j-1), and ') '


                                             ,         &N Bsp               Otherwise output "[" Print (i+1, j-1); and "]"

code such as the following:

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include < stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include < Set> #include <algorithm>using namespace std; #define LL __int64const int Inf=0x3f3f3f3f;char s[200];int dp[110] [+], Tag[110][110];int match (Char C1, char C2) {if ((c1== ' (' &&c2== ') ') | |        (c1== ' [' &&c2== '])    return 1; return 0;}    void print (int l, int r) {if (l>r) return; if (l==r) {if (s[l]== ' | | |        s[l]== ') printf ("()");    else printf ("[]");        } else if (tag[l][r]==-1) {printf ("%c", S[l]);        Print (L+1,R-1);    printf ("%c", S[r]);        } else {print (l,tag[l][r]);    Print (TAG[L][R]+1,R);    }}int Main () {int n, m, I, J, Len, K;    Gets (s);    Len=strlen (s);    if (len==0) {puts ("");    } memset (Dp,inf,sizeof (DP)); memset (tag,-1,sizeof (tag));        for (i=0;i<len;i++) {dp[i][i]=1;    dp[i+1][i]=0;            } for (i=len-2;i>=0;i--) {for (j=i+1;j<len;j++) {dp[i][j]=len+1;            if (Match (S[i],s[j])) {dp[i][j]=min (dp[i][j],dp[i+1][j-1]);                    } for (k=i;k<=j;k++) {if (Dp[i][j]>dp[i][k]+dp[k+1][j]) {                    DP[I][J]=DP[I][K]+DP[K+1][J];                Tag[i][j]=k;    }}}}//printf ("%d\n", dp[0][len-1]);        /*for (i=0;i<4;i++) {for (j=0;j<4;j++) {printf ("%d", tag[i][j]);    } puts ("");    }*/print (0,len-1);    Puts (""); return 0;}


Ural 1183 Brackets Sequence (interval dp+ memory search)

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.