POJ 1141-brackets Sequence (interval DP bracket matches print path)

Source: Internet
Author: User

Title Address: POJ 1141
Test instructions: A string consisting of a string of ' (') ' ['] ' that will be given a sequence of parentheses to be paired in the form of a minimum number of parentheses.
Idea: Dp[i][j] represents the minimum number of characters that the current subsequence needs to be added, and path stores the solution for all sub-problems. Then see the code for details.

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef__int64 LL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-7;Const intmaxn= the;CharSTR[MAXN];intPATH[MAXN][MAXN];//Storage is the best intermediate position between the character range [I,j]intDP[MAXN][MAXN];voidpath_printf (intIintJ//output sub-sequence [i,j] bracket scheme{if(I&GT;J)return;//Invalid location    if(I==J) {//Sub-sequence has only one character        if(str[i]==' ['|| str[i]=='] ')printf("[]");Else if(str[i]==' ('|| str[i]==' ) ')printf("()"); }Else if(path[i][j]==-1) {//Interval [i,j] The outermost position of the match, recursive intermediate sequence        printf("%c", Str[i]); path_printf (i+1, J-1);printf("%c", Str[j]); }Else{//otherwise recursive [i,k],[k+1,j]path_printf (I,path[i][j]); path_printf (path[i][j]+1, j); }}intMain () { while(Gets (str)) {intlen=strlen(str);if(len==0) {//There is a blank line, sad            printf("\ n");Continue; }memset(DP,0,sizeof(DP)); for(intI=0; i<len; i++)//matching of single bracketsdp[i][i]=1; for(intR=1; r<len; r++) {//R represents the length of the recursive fader sequence             for(intI=0; i<len-r; i++) {the starting position of the//enumeration sub-sequence                intJ=i+r;//Calculate the end position of the subsequenceDp[i][j]=inf;if((str[i]==' ('&&str[j]==' ) ')|| (str[i]==' ['&&str[j]=='] '))if(dp[i][j]>dp[i+1][j-1]) {dp[i][j]=dp[i+1][j-1]; path[i][j]=-1;//indicates that the brackets i,j have been matched} for(intK=i; k<j; k++)//Enumerate the best locations in the middle                    if(dp[i][j]>dp[i][k]+dp[k+1][J]) dp[i][j]=dp[i][k]+dp[k+1][j],path[i][j]=k; }} path_printf (0, len-1);puts(""); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1141-brackets Sequence (interval DP bracket matches print path)

Related Article

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.