POJ 1141 Brackets Sequence 括号匹配 区间DP

这是一道关于括号匹配的编程问题,要求找到包含给定字符序列的最短正规括号序列。使用区间动态规划方法(dp[i][j]表示区间(i,j)内所需括号数),首先检查区间首尾字符是否能匹配,若不能则在区间内寻找合适匹配位置。" 104929686,8756923,线段树模板:加法与乘法操作,"['数据结构', '线段树', '算法', '懒惰更新']
摘要由CSDN通过智能技术生成

题目描述:

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

题目分析:

该题题意就是在一个由”[]”和”()”组成的字符串中,将所有的括号进行配对(补充完整),配对规则与我们日常生活中的配对规则相同。

区间DP,用dp[i][j]表示在(i,j)这个区间内需要添加的括号数目。
在查找的时候先看区间内首字符和尾字符能否匹配,能匹配就向内匹配,若不能就在首尾之间查找那个应该和首匹配的位置。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const int MAXN = 120;
const int INF = 0x3f3f3f3f;
using namespace std;

char s[MAXN];
int dp[MAXN][MAXN];//dp[i][j]表示从i到j需要添加多少的括号
int pos[MAXN][MAXN];//pos[i][j]表示在i到j区间内与i相匹配的右括号的位置,pos[i][j]若为-1,表示i与j匹配


void print(int st,int ed)
{
    if (st>ed) return;
    if (st==ed)
    {
        if (s[st]=='(' || s[st]==')') cout<<"()";
        if (s[st]=='[' || s[st]==']') cout<<"[]";
    }
    else if (pos[st][ed]==-1)//st与ed能配对
    {
        printf("%c",s[st]);
        print(st+1,ed-1);
        printf("%c",s[ed]);
    }
    else //st与ed不能配对 找到能够配对的那个点
    {
        print(st,pos[st][ed]);
        print(pos[st][ed]+1,ed);
    }
}

int main()
{
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0; i<len; i++) dp[i][i]=1;
    for(int l=2; l<=len; l++)//l为长度
    {
        for(int i=0; i<len-l+1; i++)//i为开始位置
        {
            int j=i+l-1;//终止位置
            if ((s[i]=='(' && s[j]==')') || (s[i]=='[' && s[j]==']'))
            {
                dp[i][j]=dp[i+1][j-1];//两端能配对,向内寻找
                pos[i][j]=-1;
            }
            else dp[i][j]=INF;
            for(int k=i; k<j; k++)//一端的配对在这一段中间,寻找这个位置
            {
                if (dp[i][j]>dp[i][k]+dp[k+1][j])
                {
                    dp[i][j]=dp[i][k]+dp[k+1][j];
                    pos[i][j]=k;
                }
            }
        }
    }
    print(0,len-1);//递归输出
    printf("\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值