Test instructions
Now we define an R string that must meet the following conditions:
1) when its string is empty, then it is the R string.
2) when it is R string, then (s) or [s] is also the R string.
3) when both A and B are r strings, then AB is also the R string.
I did not fully understand the meaning of the topic here, so I found that the recursion is not over. In fact, its essence is the parentheses match.
This means that the legal sequence here is that the parentheses can match 22.
if ((a[s]== ' (' &&a[e]== ') ') | | (a[s]== ' [' &&a[e]== ']) This is the case when the perimeter is matched to each other.
dp[s][e]=dp[s+1][e-1]+2;
When the perimeter doesn't match, then we're going to enumerate the split points. There is not much difference between this and the interval DP.
#include <stdio.h> #include <string.h> #include <map> #include <set> #include <queue># include<stack> #include <math.h> #include <iostream> #include <algorithm>using namespace std;# Define MAXN 111char a[maxn];int dp[maxn][maxn];int main () {while (~SCANF ("%s", a)) {if (strcmp (A, "end") ==0) Break;memset ( Dp,0,sizeof (DP)); int L=strlen (a); for (int. len=2;len<=l;len++) {for (int s=0;s<=l-len;s++) {int e=s+len-1;if (a[s ]== ' (' &&a[e]== ') ') | | (a[s]== ' [' &&a[e]== ']) dp[s][e]=dp[s+1][e-1]+2;for (int k=s;k<e;k++) {Dp[s][e]=max (dp[s][e],dp[s][k]+dp[k+1][e]);}}} int ans=0;for (int i=0;i<l;i++) {for (int j=0;j<l;j++) {Ans=max (ans,dp[i][j]);}} printf ("%d\n", ans);}}
Hope that the thinking can be more progress, refueling refueling!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ (2955)--brackets (interval DP)