POJ 1141 Brackets Sequence(动态规划 / 递归)
Brackets Sequence (POJ 1141)
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33360 Accepted: 9662 Special Judge
Description
Let us define a regular brackets sequence in the following way:
- Empty sequence is a regular sequence.
- If S is a regular sequence, then (S) and [S] are both regular sequences.
- 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
()[()]
第一眼看到这道题直觉得考的是DP,但我没想到要怎么打印。然后参考了别人的博客,才知道用pos[][]来存储最优分离点的坐标。然后递归打印pos分割的串。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 256;
char str[MAXN];
int dp[MAXN][MAXN],pos[MAXN][MAXN];
int l,r;
bool h(char a,char b)
{
if(a == '(' && b == ')')return true;
if(a == '[' && b == ']')return true;
return false;
}
void print(int i, int j)
{
if(i > j)return ;
if(i == j)
{
if(str[i] == '(' || str[j] == ')')
{
printf("()");
}
else
{
printf("[]");
}
}
else if(pos[i][j] == -1)
{
printf("%c",str[i]);
print(i+1,j-1);
printf("%c",str[j]);
}
else
{
print(i,pos[i][j]);
print(pos[i][j] + 1,j);
}
}
int main()
{
while(gets(str))
{
int len;
len = strlen(str);
memset(dp,0,sizeof(dp));
for(int i = 0; i < len; i++)
{
dp[i][i] = 1;
}
int j;
for(int k = 1; k < len; k++)
{
for(int i = 0; i + k < len; i++)
{
j = i + k;
dp[i][j] = 0x7fffffff;
if(h(str[i],str[j]))
{
dp[i][j] = dp[i+1][j-1];
pos[i][j] = -1;
}
for(int mid = i; mid <j; mid++)
{
if(dp[i][j] > dp[i][mid] + dp[mid+1][j])
{
dp[i][j] = dp[i][mid] + dp[mid+1][j];
pos[i][j] = mid;
}
}
}
}
print(0,len-1);
printf("\n");
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
文章来源: blog.csdn.net,作者:爱玲姐姐,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jal517486222/article/details/79342521
- 点赞
- 收藏
- 关注作者
评论(0)