Question Link: Http://poj.org/problem? Id = 2955
Theme: Matching brackets. The number of symmetric parentheses matches + 2. Maximum number of matches.
Solutions:
It looks like a range problem.
DP border: none. When the interval is 0, the default value is memset 0.
For DP [I] [J], if I and j match, it is not difficult to have DP [I] [J] = DP [I + 1] [J-1] + 2.
Then enumeration does not belong to the midpoint at both ends, DP [I] [J] = max (DP [I] [J], DP [I] [k] + dp [k] [J]), and merge the results of the two intervals.
#include "cstdio"#include "string"#include "cstring"#include "iostream"using namespace std;bool check(char a,int b){ if(a==‘(‘&&b==‘)‘) return true; else if(a==‘[‘&&b==‘]‘) return true; else return false;}int dp[105][105];int main(){ //freopen("in.txt","r",stdin); string str; while(cin>>str&&str!="end") { int n=str.size(); for(int p=1;p<n;p++) { for(int i=0;i<n-p;i++) { int j=i+p; if(check(str[i],str[j])) dp[i][j]=dp[i+1][j-1]+2; for(int k=i+1;k<j;k++) dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]); } } printf("%d\n",dp[0][n-1]); memset(dp,0,sizeof(dp)); }}
Poj 2955 (interval DP)