Topic Links:
Poj 2955 Brackets
Title Description:
Given a string of parentheses, what is the number of parentheses with the maximum legal match?
Problem Solving Ideas:
Interval Dp,dp[x][y] represents the maximum number of matches in the interval [x, y), and then enumerates the interval lengths and start points. Consider that for parentheses the parentheses can be converted by state [x+1,y-1], and parentheses can be converted by [X, k) + [k, y) (x<k<y). State transfer OK down after you can happily play code spicy!
1#include <cmath>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 typedef __int64 LL;8 Const intMAXN = the;9 intDP[MAXN][MAXN];Ten One intMain () A { - CharSTR[MAXN]; - while(SCANF ("%s", str), strcmp (str,"End")) the { -Memset (DP,0,sizeof(DP)); - intLen =strlen (str); - + for(intI=2; i<=len; i++) - for(intj=0; j+i<=len; J + +) + { ADp[j][j+i] = dp[j+1][j+i-1]; at if(Str[j] = ='('&& str[j+i-1] ==')') -Dp[j][j+i] + +; - if(Str[j] = ='['&& str[j+i-1] ==']') -Dp[j][j+i] + +; - for(intk=j+1; k<=j+i; k++) -Dp[j][j+i] = max (Dp[j][j+i], dp[j][k]+dp[k][j+i]); in } -printf ("%d\n", dp[0][len] *2); to } + return 0; -}
Poj 2955 Brackets (interval dp)