POJ (2955)--brackets (interval DP)

Source: Internet
Author: User

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)

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.