Uva1626-brackets sequence (interval dp--bracket matching + recursive printing)

Source: Internet
Author: User

Title Description:

Define the valid parentheses sequence as follows:

1 empty sequence is a valid sequence

2 If S is a valid sequence, then (s) and [s] are also valid sequences

3 If A and B are valid sequences, then AB is also a valid sequence

For example: The following are the valid parentheses sequence

(),  [],  (()),  ([]),  ()[],  ()[()]

The following are all illegal parenthesis sequences.

(,  [,  ),  ) (,  ([)],  ([(]

Given a sequence consisting of ' (', ') ', ' [', ' and '] ', find the shortest legal sequence with that sequence as a subsequence.

Idea: Really classic topic, interval DP, the topic actually has a trap, input may be empty string, so with scanf time, will not read into, less once read, WA

So with the gets

acceptedc++1.0022015-03-12 13:34:47#include<cstdio> #include <cstring> #include <iostream># include<algorithm>using namespace Std;const int inf= 0x3f3f3f3f;int Dp[105][105];char str[105];int n;bool Match (    Char A,char b) {if (a== ' (' &&b== ') ') return true;    else if (a== ' [' && b== '] ') return true; return false;}    void print (int l,int r)//recursive print solution {if (l>r) return; if (l==r) {if (str[l]== ' | | |        str[l]== ') printf ("()"); else if (str[l]== ' [' | | |        str[l]== '] ') printf ("[]");    return; } if (Match (Str[l],str[r]) &&dp[l][r]==dp[l+1][r-1])//Do not forget match (Str[l],str[r]), because dp[l][r]==dp[l+1][r-1] time,        Does not necessarily match outside brackets, it is easy to err {Putchar (str[l]);        Print (L+1,R-1);        Putchar (Str[r]);    return;            } for (int k=l;k<r;k++) if (Dp[l][r]==dp[l][k]+dp[k+1][r]) {print (l,k);            Print (K+1,R);        Return    }}int Main () {int T;    scanf ("%d", &t); GetChar ();//Eat a newline after T    while (t--) {GetChar ();//There is a blank line before each input (str+1);        N=strlen (str+1);          Memset (Dp,0,sizeof (DP)); for (int i=1;i<=n;i++) dp[i][i]=1;                Boundary for (int l=1;l<n;l++) for (int p=1;p+l<=n;p++) {dp[p][p+l] = inf;                if (Match (Str[p],str[p+l])) DP[P][P+L]=DP[P+1][P+L-1];            for (int k=p; k<p+l; k++) dp[p][p+l]=min (Dp[p][p+l],dp[p][k]+dp[k+1][p+l]);        } if (n) print (1,n);        Puts (""); if (T) Putchar (' \ n '); Output a blank line between outputs} return 0;}


Uva1626-brackets sequence (interval dp--bracket matching + recursive printing)

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.