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