Brackets Sequence Bracket DP

Source: Internet
Author: User

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

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.