"POJ 1141" Brackets Sequence

Source: Internet
Author: User

Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27996 Accepted: 7936 Special Judge

Description

Let us define a regular brackets sequence in the following by:

1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (s) and [s] are both regular sequences.
3. If A and B are regular sequences, then AB is A regular sequence.

For example, all of the following sequences of characters is regular brackets sequences:

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

And all of the following character sequences is not:

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

Some sequence of characters ' (', ') ', ' [', and '] ' is given. You is to find the shortest possible regular brackets sequence, that contains the given character sequence as a Subsequen Ce. Here, a string A1 A2 ... is called a subsequence of the string B1 b2 ... bm, if there exist such indices 1 = I1 < I2 < ... < in = m, which AJ = bij for all 1 = j = N.

Input

The input file contains at most of brackets (characters ' (', ') ', ' [' and '] ') that is situated on a single line without Any other characters among them.

Output

Write to the output file A contains some regular brackets sequence this has the minimal possible length a nd contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

Source

Northeastern Europe 2001 can no longer believe POJ special judge, in fact there is no good felling. Got me on my knees ... A test data to see ... Cough cough. Well, the answer to this question, in fact, is the interval dp+ answers output. The recursive output of the answer is also relatively straightforward. The DP equation can be found in the Rujia Teacher's Black book, which is not mentioned here.
#include <cstdio>#include<cstring>using namespacestd;Chars[ the];intN, f[ the][ the], p[ the][ the];voidPrintintLintR) {    if(L > R)return; if(P[l][r] = =-1) {printf ("%c", S[l]); Print (L+1, R-1); printf ("%c", S[r]); return; }    if(P[l][r] = =-2) {printf ("%c", S[l]); Print (L+1, R); if(S[l] = ='(') printf (")"); Elseprintf"]"); return; }    if(P[l][r] = =-3)    {        if(S[r] = =')') printf ("("); Elseprintf"["); Print (L, R-1); printf ("%c", S[r]); return; } print (L, P[l][r]-1); Print (P[l][r], R);}intMain () {fgets (s),104, stdin); N= strlen (s)-1; Memset (F,0x7f,sizeof(f)); Memset (P,0,sizeof(p));  for(inti =0; I < n; ++i) {F[i][i]=1; if(S[i] = ='('|| S[i] = ='[') P[i][i] =-2; ElseP[i][i] =-3;  for(intj =0; J < I; ++j) F[i][j]=0; }     for(inti = n-1; I >=0; --i) for(intj = i +1; J < N; ++j) {if((s[i] = ='('|| S[J] = ='[') && F[i][j] > f[i +1][J] +1) {F[i][j]= F[i +1][J] +1; P[I][J]= -2; }            if((s[j] = =')'|| S[J] = =']') && F[i][j] > F[i][j-1] +1) {F[i][j]= F[i][j-1] +1; P[I][J]= -3; }            if((s[i] = ='('&& S[j] = =')') || (S[i] = ='['&& S[j] = =']')) && F[i][j] > f[i +1][j-1]) {F[i][j]= F[i +1][j-1]; P[I][J]= -1; }             for(intK = i +1; K <= J; ++k)if(F[i][j] >= f[i][k-1] +F[k][j]) {F[i][j]= F[i][k-1] +F[k][j]; P[I][J]=K; }} print (0N1); printf ("\ n"); return 0;}

"POJ 1141" Brackets Sequence

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.