Brackets Sequence
Topic Abstraction: Give you a parenthesis string that asks you to add the best parentheses to match the parentheses. Enter the string of parentheses after the match.
Analysis: A large interval is introduced by a known sub-range. CNT[I][J] Indicates the number of parentheses in the original string [i,j] that need to be added at least. ANS[I][J] Stores the string after the original string [I,j] matches.
Note that the input is an empty string. An empty string is a matching string.
1 ImportJava.util.*;2 ImportJava.io.*;3 Importjava.math.*;4 5 Public classMain6 {7 StaticScanner cin =NewScanner (NewBufferedinputstream (system.in));8 Public Final Static intMs= 105;9 Static int[] cnt =New int[Ms][ms];Ten Staticstring[][] ans =NewString[ms][ms]; One A - Public Static voidMain (string[] args) - { theString str =cin.nextline (); -Str.replaceall ("", "" "); - if(str.length () = = 0) - { + System.out.println (); -System.exit (0); + } A Char[] ch =Str.tochararray (); at intLen =ch.length; - - for(inti = 0; i<len;i++) - for(intj = i;j< len;j++) -CNT[I][J] =Integer.max_value; - for(inti = 0;i<len;i++) inArrays.fill (Ans[i], ""); - to for(inti = len-1;i>=0;i--) + { - for(intj = i;j<len;j++) the { * if(i = =j) $ {Panax NotoginsengCNT[I][J] = 1; - if(Ch[i] = = ' (' | | ch[i] = = ') ') theAns[i][i] = "()"; + Else AAns[i][i] = "[]"; the } + Else - { $ if(Ch[i] = = ' (' && ch[j] = = ') ') $ { - if(Cnt[i+1][j-1] <Cnt[i][j]) - { theCNT[I][J] = cnt[i+1][j-1]; -ANS[I][J] = "(" + ans[i+1][j-1] + ")";Wuyi the } - } Wu Else if(Ch[i] = = ' [' && ch[j] = = '] ') - { About if(Cnt[i+1][j-1] <Cnt[i][j]) $ { -CNT[I][J] = cnt[i+1][j-1]; -ANS[I][J] = "[" + ans[i+1][j-1] + "]"; - } A } + //the minimum value for CNT[I][J]. the for(intK = i;k<j;k++) - { $ if(Cnt[i][k] + cnt[k+1][j] <Cnt[i][j]) the { theCNT[I][J] = Cnt[i][k] + cnt[k+1][j]; theANS[I][J] = Ans[i][k] + ans[k+1][j]; the } - } in } the } the } AboutSystem.out.println (ans[0][len-1]); the } the}
Brackets Sequence Bracket DP