POJ 2955 interval DP Brackets

Source: Internet
Author: User

To find the maximum number of matches for a brace, this question can be compared to UVA 1626.

Pay attention to the same background, but do not ask for the same.

Back to this question, set D (i, j) to denote the maximum number of matches in a substring of SI ~ sj, if Si and SJ can pair, D (i, j) = d (i+1, j-1)

Then to enumerate the intermediate points k,d (i, j) = max{D (i, K) + D (k+1, J)}

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intMAXN = -+Ten;8 9 intN;Ten CharS[MAXN]; One intD[MAXN][MAXN]; A  - BOOLInline Match (CharC1,CharC2) - { the     if(C1 = ='['&& C2 = =']')return true; -     if(C1 = ='('&& C2 = =')')return true; -     return false; - } +  - intMain () + { A      while(SCANF ("%s", s) = =1&& s[0] !='e') at     { -n =strlen (s); -          for(inti =0; i +1< n; i++) -         { -             if(Match (S[i], s[i+1])) d[i][i+1] =2; -             Elsed[i][i+1] =0; in         } -  to          for(intL =3; L <= N; l++) +         { -              for(inti =0; i + L-1< n; i++) the             { *                 intj = i + L-1; $D[I][J] =0;Panax Notoginseng                 if(Match (S[i], s[j])) d[i][j] = d[i+1][j-1] +2; -                  for(intK = i; K < J; k++) theD[I][J] = max (D[i][j], d[i][k] + d[k+1][j]); +             } A         } the  +printf"%d\n", d[0][n-1]); -     } $  $     return 0; -}
code June

POJ 2955 interval DP Brackets

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.