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.
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
- Brackets Sequence (P1141)
- Brackets Sequence
- Brackets Sequence
- Brackets sequence
- Brackets Sequence
- Brackets Sequence
- Brackets Sequence
- DP之Brackets Sequence
- ZOJ 1463 Brackets Sequence
- PKU1141 Brackets Sequence
- POJ 1141 Brackets Sequence
- pku 1141 Brackets Sequence
- POJ1141 Brackets Sequence
- POJ 1141 Brackets Sequence
- Brackets Sequence--poj--1141
- PKU1141 brackets sequence
- poj 1141 Brackets Sequence
- poj 1141 Brackets Sequence
- 人吃人
- 深入理解js面向对象中的prototype
- 关于Android的Run as 不出现Android Application的问题
- SPOJ 1043
- 【100题】求1+2+……N的和(变态级限制)
- Brackets Sequence (P1141)
- java值传递以及引用传递
- 替换字符串中部分字符串,java
- 枚举的应用
- 实习总结(九)---黑客技术小盘点+
- Android 通话处理流程
- 解析PHP程序在Windows Azure内运行步骤
- gcc 增加 include 目录
- PHP生成静态html文件 的三种方法