Brackets Sequence (P1141)

来源:互联网 发布:smo算法 python 编辑:程序博客网 时间:2024/06/05 20:25

这个题求出要加多少个半括号简单,难点在于要补充完整。自己做了好长时间,各种bug


主要的思想是输出时根据记录的以同样的迭代方式输出。


#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<set>#include<cstdlib>#include<cstring>using namespace std;#define N 100000int n;char s[1111];int pre[111][111];int ans;int d[111][111];int dp(int i,int j){if (i>j)return 0;if (i==j){if (s[i]==')')pre[i][j]=1;else if (s[i]==']')pre[i][j]=2;else if (s[i]=='(')pre[i][j]=3;else pre[i][j]=4;return d[i][j]=1;}if (d[i][j]<N){return d[i][j];}int k;if (s[i]=='('&&s[j]==')'){if (d[i][j]>dp(i+1,j-1)){d[i][j]=dp(i+1,j-1);pre[i][j]=5;//cout<<i<<' '<<j<<' '<<pre[i][j]<<endl;}}if (s[i]=='['&&s[j]==']'){if (d[i][j]>dp(i+1,j-1)){d[i][j]=dp(i+1,j-1);pre[i][j]=5;}}if (s[i]=='('){if (d[i][j]>dp(i+1,j)+1){d[i][j]=dp(i+1,j)+1;pre[i][j]=3;}}if (s[i]=='['){if (d[i][j]>dp(i+1,j)+1){d[i][j]=dp(i+1,j)+1;pre[i][j]=4;}}if (s[j]==')'){if (d[i][j]>dp(i,j-1)+1){d[i][j]=dp(i,j-1)+1;pre[i][j]=1;}}if (s[j]==']'){if (d[i][j]>dp(i,j-1)+1){d[i][j]=dp(i,j-1)+1;pre[i][j]=2;}}for (k=i;k<j;k++){if (d[i][j]>dp(i,k)+dp(k+1,j)){d[i][j]=dp(i,k)+dp(k+1,j);pre[i][j]=0;//cout<<i<<' '<<k<<' '<<j<<endl;}}return d[i][j];}void put(int i,int j){//cout<<i<<' '<<j<<' '<<pre[i][j]<<endl;if (i==j){if (s[i]=='(' || s[i]==')')cout<<"()";else cout<<"[]";return ;}if (i>j)return ;if (pre[i][j]==5){cout<<s[i];put(i+1,j-1);cout<<s[j];}else if (pre[i][j]==1 ) {cout<<'(';put(i,j-1);cout<<')';}else if (pre[i][j]==2){cout<<'[';put(i,j-1);cout<<']';}else if (pre[i][j]==3){cout<<'(';put(i+1,j);cout<<')';}else if (pre[i][j]==4){cout<<'[';put(i+1,j);cout<<']';}else {int k;for (k=i;k<j;k++){if (d[i][j]==d[i][k]+d[k+1][j]){put(i,k);put(k+1,j);break;}}}}int main(){freopen("in.txt","r",stdin);int i,j,k;gets(s);n=strlen(s);memset(pre,0,sizeof(pre));for (i=0;i<n;i++){for (j=0;j<n;j++)d[i][j]=N;}ans=dp(0,n-1);//cout<<d[0][n-1]<<endl;i=0;j=1;if (d[0][n-1]==0){cout<<s<<endl;return 0;}put(0,n-1);cout<<endl;return 0;}





Brackets Sequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19310 Accepted: 5329 Special Judge

Description

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:

(), [], (()), ([]), ()[], ()[()]

And all of the following character sequences are not:

(, [, ), )(, ([)], ([(]

Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.

Input

The input file contains at most 100 brackets (characters '(', ')', '[' and ']') that are situated on a single line without any other characters among them.

Output

Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()]

Source

Northeastern Europe 2001