P1241 Bracket Sequence Title Description
Define the following sequence of rules (string):
1. An empty sequence is a sequence of rules;
2. If S is a sequence of rules, then (s) and [s] are also regular sequences;
3. If both A and B are regular sequences, then AB is also the rule sequence.
For example, the following string is a sequence of rules:
(),[],(()),([]),()[],()[()]
And the following are not:
(,[,],) (,()),([()
Now, to give you some of the sequences made up of ' (', ') ', ' [', '] ', what you have to do is to find a sequence of shortest rules, so that the sequence given to you is the child of the sequence of rules you give. (for sequence A1,a2,...,an and sequence BL,B2,...,BM, if there is a set of subscript 1≤i1<i2<...<in≤m that makes Aj=b (I,J) The 1≤j≤n of everything, then A1,a2...,an is called the B1,B2,...,BM's child column.
Input output Format input format:
Input file only one line, all by ' (', ') ', '] ', '] ' composition, no other characters, the length of not more than 100.
Output format:
The output file also has only one row, all consists of ' (', ') ', '] ', '] ', no other characters, the output of the regular sequence you find. Because the rule sequence may be more than one, the number of nested layers in the rule sequence that is required to output is as low as possible.
Input and Output Sample input example # #:
([()
Sample # # of output:
()[]()
Description
Output Explanation:
{The maximum number of nested layers is 1, such as () [()] If the number of layers is 2 o'clock
jsoi2011
/*69-Point maintenance stack*/#include<cstdio>#include<iostream>#include<cstring>using namespacestd;Const intmaxn= -;Const intinf=0x7fffffff;intF[MAXN][MAXN],A[MAXN][MAXN];CharS[MAXN];intN;voidaaaaintXinty) { if(x>y)return; if(x==y) {if(s[x]=='('|| s[x]==')') printf ("()"); Elseprintf ("[]"); } Else { if(a[x][y]==-1) { if(s[x]=='(') {printf ("("); AAAA (x+1, Y1); printf (")"); } Else{printf ("["); AAAA (x+1, Y1); printf ("]"); } } Else{AAAA (x,a[x][y]); AAAA (a[x][y)+1, y); } } } intMain () {//gets (s);scanf"%s",&s); N=strlen (s); Memset (F,0,sizeof(f)); for(intI=n;i>0; i--) {S[i]=s[i-1]; F[i][i]=1; } inttot; for(intp=1;p <=n;p++) { for(intI=1; i<=n-p;i++) { intj=i+p; F[I][J]=INF; if((s[i]=='('&&s[j]==')')|| (s[i]=='['&&s[j]==']') ) {tot=f[i+1][j-1]; if(f[i][j]>tot) f[i][j]=tot; } A[i][j]=-1; for(intk=i;k<j;k++) {tot=f[i][k]+f[k+1][j]; if(f[i][j]>tot) {F[i][j]=tot; A[I][J]=K; } }}} AAAA (1, N); return 0;}
Rokua P1241 Bracket Sequence