Bracket Matching (ii)--classic dynamic programming

Source: Internet
Author: User

The parentheses here match, and if two are the same then execute the following statement
if (CMP (Str[i],str[j]))                       = Min (dp[i][j],dp[i+1][j-1]);

each time you determine the parentheses that need to be filled from I to J, this value is
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <iostream>5#include <limits.h>6#include <algorithm>7#include <queue>8#include <vector>9#include <Set>Ten#include <stack> One#include <string> A#include <sstream> -#include <map> -#include <cctype> the using namespacestd; - intdp[ the][ the]; - BOOLcmpintNintm) - { +     if(n = ='('&&m = =')')|| (n = ='['&&m = =']')) -         return true; +     return false; A } at intMainvoid) - {  -     intn,m,i,j,k; -     Charstr[101]; -scanf"%d",&n); -      while(n--) in     { -scanf"%s", str); to         intLength =strlen (str); +Memset (DP,0,sizeof(DP)); -          for(i =0; i < length; i++) theDp[i][i] =1; *          for(M =1; m < length; m++)//let's first calculate the difference between the two numbers. $         {Panax Notoginseng              for(i =0; i < length-m; i++) -             { thej = i + M;//J and I difference M +DP[I][J] = the;//the default between I and J requires 105 parentheses to fill A                 if(CMP (STR[I],STR[J]))//See if I and J are compatible theDp[i][j] = min (dp[i][j],dp[i+1][j-1]);//the matching words from the last and +                  for(k = i; k < J; k++) -                 { $Dp[i][j] = min (dp[i][j],dp[i][k]+dp[k+1][j]); $                 } -             } -         } theprintf"%d\n", dp[0][length-1]); -     }Wuyi     return 0; the}

Bracket Matching (ii)--classic dynamic programming

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.