Let us define a regular brackets sequence in the following way:
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 are regular brackets sequences:
(), [], (), ([]), () [], () [()]
And all of the following character sequences are not:
(, [,),) (, ([)], ([(]
Some sequence of characters '(', ')', '[', and'] 'is given. you are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. here, a string A1 A2... an is called a subsequence of the string B1 B2... BM, if there exist such indices 1 = I1 <I2 <... <in = m, That Aj = bij for all 1 = J = n.
Input
The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.
Output
Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. each input block is in the format indicated in the Problem description. there is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
([(]
Sample output
() [()]
The key lies in the input and output formats!
Interval DP, DP [I] [J] indicates the number of matches between interval I and J. Can the characters at both ends of the interval match exactly, if the status transfer can be matched, a DP [I] [J] = max (DP [I] [k] + dp [k + 1] [J] is added. DP [I + 1] [J-1] + 1), if not match is DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k + 1] [J]);
If the two ends can match and the two ends match, the maximum Dp value is displayed. MARK [I] [J] =-1, otherwise, Mark [I] [J] = K. In this way, DP is applied to all intervals, and then DFS is used for searching, if the matching at both ends leads to the largest value, mark the character and continue searching for smaller intervals. Otherwise, the two intervals are separated for [I, K] [k + 1, j]
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i<=y;i++)#define mem(a,b) memset(a,b,sizeof(a))#define w(a) while(a)char str[105];int t,len,dp[105][105],mark[105][105],pos[105];void dfs(int i,int j){ if(mark[i][j]==-1) { pos[i]=pos[j]=1; dfs(i+1,j-1); } else if(mark[i][j]>=0) { dfs(i,mark[i][j]); dfs(mark[i][j]+1,j); } return;}int main(){ int l,i,j,k; scanf("%d%*c%*c",&t); while(t--) { gets(str); len=strlen(str); if(!len) { printf("\n"); if(t) printf("\n"); continue; } up(i,0,len-1) up(j,0,len-1) { mark[i][j]=-2; dp[i][j]=0; } mem(pos,0); i=j=l=0; w(l<len) { if(i==j) { i++,j++; if(j==len) i=0,l++,j=l; continue; } if((str[i]=='('&&str[j]==')')||(str[i]=='['&&str[j]==']')) { up(k,i,j-1) { if(dp[i][j]<dp[i][k]+dp[k+1][j]) { mark[i][j]=k; dp[i][j]=dp[i][k]+dp[k+1][j]; } } if(dp[i][j]<dp[i+1][j-1]+1) { mark[i][j]=-1; dp[i][j]=dp[i+1][j-1]+1; } } else { up(k,i,j-1) { if(dp[i][j]<dp[i][k]+dp[k+1][j]) { mark[i][j]=k; dp[i][j]=dp[i][k]+dp[k+1][j]; } } } i++,j++; if(j==len) { l++; i=0; j=l; } } dfs(0,len-1); up(i,0,len-1) { if(pos[i]==1) printf("%c",str[i]); else if(str[i]=='('||str[i]==')') printf("()"); else printf("[]"); } printf("\n"); if(t) { printf("\n"); getchar(); } } return 0;}