POJ 1141 Brackets Sequence(动态规划 / 递归)

举报
楚楚冻人玥玥仙女 发表于 2021/11/19 01:25:57 2021/11/19
【摘要】 Brackets Sequence (POJ 1141) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33360 Accept...

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:

  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

()[()]

第一眼看到这道题直觉得考的是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

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。