标签:des style blog io color os ar for sp
Description
Input
Output
Sample Input
([(]
Sample Output
()[()]
区间DP、重点在输出
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) #define INF 0x7fffffff #define N 110 char s[N]; int ss[N][N]; //ss[i][j]=k,i到j从k位置分开添加的括号数最少 int dp[N][N]; //dp[i][j]是在i~j区间最多括号匹配数 int judge(char c1,char c2) { if(c1==‘(‘ && c2==‘)‘) return 1; if(c1==‘[‘ && c2==‘]‘) return 1; return 0; } void print(int i,int j) { if(i>j) return; else if(i==j) { if(s[i]==‘(‘ || s[i]==‘)‘) cout<<"()"; else cout<<"[]"; } else { if(ss[i][j]==-1) { cout<<s[i]; print(i+1,j-1); cout<<s[j]; } else { print(i,ss[i][j]); print(ss[i][j]+1,j); } } } int main() { int n,i,j,k,len; gets(s+1); n=strlen(s+1); for(i=1;i<=n;i++) { dp[i][i]=1; } for(len=2;len<=n;len++) { for(i=1;i<=n-len+1;i++) { j=i+len-1; dp[i][j]=INF; for(k=i;k<j;k++) { if(dp[i][j]>dp[i][k]+dp[k+1][j]) { ss[i][j]=k; dp[i][j]=dp[i][k]+dp[k+1][j]; } } if(judge(s[i],s[j]) && dp[i][j]>dp[i+1][j-1]) { ss[i][j]=-1; dp[i][j]=dp[i+1][j-1]; } } } //cout<<dp[1][n]<<endl; print(1,n); printf("\n"); return 0; }
区间DP [POJ 1141] Brackets Sequence
标签:des style blog io color os ar for sp
原文地址:http://www.cnblogs.com/hate13/p/4064062.html