POJ 2955 Brackets & POJ 1505 Copying Books & POJ 1651 multiplication Puzzle (primary interval DP)

Source: Internet
Author: User
POJ 2955 Brackets Main Topic

The number of matching parentheses in the given string, () and [] are matched. Thinking of solving problems

DP[I][J] represents how many matching strings are available for i~j.
Transfer equation:
1, Dp[i][j]=dp[i+1][j-1]+2, when the i,j position composition can match the parentheses;
2. Enumeration split point K (I≤k<j) (I \le K:dp[i][j]=max (dp[i][j],dp[i][k]+dp[k+1][j]) code implementation

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define MAXN 107
int DP[MAXN][MAXN];
Char STR[MAXN];
int main ()
{
    while (gets (str)!=null)
    {
        if (str[0]== ' e ')
        is break; Memset (Dp,0,sizeof (DP));
        int Length=strlen (str);
        for (int i=1;i<length;i++)
        {for
            (int s=0,e=i;e<length;e++,s++)
            {
                if (str[s]== ' (' & &str[e]== ') | | (str[s]== ' [' &&str[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]);
            }
        }
        printf ("%d\n", Dp[0][length-1]);
    }
    return 0;
}
POJ 1505 Copying Books Main Topic

Divides the number of rows into successive K-sections so that the maximum values of each part are as small as possible. Thinking of solving problems

Dp[i][j] Represents the maximum number of pages that J has completed before I, by Sum[i], representing the total number of pages of the previous I.
Transfer equation:
1, dp[i][1]=num[i];
2, Enumeration Division point K (J−1≤k<i) (J-1\le K, so that the former K by j-1 people, the remaining k+1~i is completed by one person, then Dp[i][j]=min (Dp[i][j],max (dp[k][j-1],sum[i]-sum[ K])). Code Implementation

#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> using namespace std
;
#define MAXN 507 int DP[MAXN][MAXN],SUM[MAXN],MARK[MAXN];
    int main () {int T;
    int m,k,t;
    scanf ("%d", &t);
        while (t--) {memset (sum,0,sizeof (sum));
        Memset (Dp,-1,sizeof (DP));
        memset (Mark,0,sizeof (Mark));
        scanf ("%d%d", &m,&k);
            for (int i=1; i<=m; i++) {scanf ("%d", &t);
        SUM[I]=T+SUM[I-1];
        } dp[0][0]=0;
                for (int i=1, i<=m; i++) {for (int j=1; j<=i&&j<=k; J + +) {
                if (j==1) dp[i][j]=sum[i]; else for (int k=j-1; k<i; k++) {int Z=max (DP[K][J-1],SU
                        M[i]-sum[k]); if (dp[i][j]==-1| |
                    Z&LT;DP[I][J]) dp[i][j]=z;
      }
            }
        }  int j=k-1,num=0;
            for (int i=m; i>=1; i--) {num+= (sum[i]-sum[i-1]); if (num>dp[m][k]| |
                I&LT;=J) {mark[j--]=i+1;
            NUM=SUM[I]-SUM[I-1]; }} for (int i=1,j=1;i<=m;i++) {printf (i==m? ")
            %d\n ":"%d ", sum[i]-sum[i-1]);
                if (mark[j]==i+1) {printf ("/");
            j + +;
}}} return 0;
 }
POJ 1651 multiplication Puzzle Main Topic

Take one number at a time in N, the number is multiplied by the number of the left and right sides, then add each time, only the first and last number is not desirable, n-2 after the operation and the possible minimum value. Thinking of solving problems

DP[I][J] represents the possible minimum value of the I~j interval.
Transfer equation:
1, dp[i][i+2]=a[i]*a[i+1]*a[i+2], when the interval length is 3 o'clock;
2, Enumeration Division point K (I+1≤k<j) (I+1\le K, now the left interval [i,k] to complete the fetch, and then in the right interval [k,j] to complete the fetch, and finally take the number of K-position, at this time the number of the left and right sides is a[i] and a[j], then Dp[i][j]=min ( DP[I][J],DP[I][K]+DP[K][J]+A[I]*A[K]*A[J]) When the interval length is >=4. Code Implementation

 #include <iostream> #include <cstdio> #include <cstring> using namespace
Std
#define MAXN 107 #define INF 0x3f3f3f3f int DP[MAXN][MAXN],A[MAXN];
    int main () {int n;
        while (~SCANF ("%d", &n)) {memset (dp,0,sizeof (DP));
        for (int i=1; i<=n; i++) scanf ("%d", &a[i]);
        for (int i=1; i+2<=n; i++) dp[i][i+2]=a[i]*a[i+1]*a[i+2]; for (int len=4, len<=n; len++) {for (int i=1; i+len-1<=n; i++) {int
                j=i+len-1;
                Dp[i][j]=inf;
            for (int k=i+1; k<j; k++) dp[i][j]=min (Dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
    }} printf ("%d\n", Dp[1][n]);
} return 0; }

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.