首页 > 代码库 > BNUOJ 1260 Brackets Sequence
BNUOJ 1260 Brackets Sequence
Brackets Sequence
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 114164-bit integer IO format: %lld Java class name: Main
Special Judge
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
解题:这dp啊,我这种学渣啊,每做一次,就有一次新的感觉!水好深啊!
注意是单样例的!改成多样例,立马WA了
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 110;18 int dp[maxn][maxn],c[maxn][maxn] = {-1};19 char str[maxn];20 void print(int i,int j) {21 if(i > j) return;22 if(i == j) {23 if(str[i] == ‘(‘ || str[j] == ‘)‘)24 printf("()");25 else printf("[]");26 } else {27 if(c[i][j] >= 0) {28 print(i,c[i][j]);29 print(c[i][j]+1,j);30 } else {31 if(str[i] == ‘(‘) {32 printf("(");33 print(i+1,j-1);34 printf(")");35 } else {36 printf("[");37 print(i+1,j-1);38 printf("]");39 }40 }41 }42 }43 void go() {44 int len = strlen(str),i,j,k,theMin,t;45 for(i = 0; i < len; i++) dp[i][i] = 1;46 for(k = 1; k < len; k++) {47 for(i = 0; i+k < len; i++) {48 j = i+k;49 theMin = dp[i][i]+dp[i+1][j];50 c[i][j] = i;51 for(t = i+1; t < j; t++) {52 if(dp[i][t]+dp[t+1][j] < theMin) {53 theMin = dp[i][t]+dp[t+1][j];54 c[i][j] = t;55 }56 }57 dp[i][j] = theMin;58 if(str[i] == ‘(‘ && str[j] == ‘)‘ || str[i] == ‘[‘ && str[j] == ‘]‘) {59 if(dp[i+1][j-1] < theMin) {60 dp[i][j] = dp[i+1][j-1];61 c[i][j] = -1;62 }63 }64 }65 }66 print(0,len-1);67 }68 int main() {69 scanf("%s",str);70 go();71 puts("");72 return 0;73 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。