POJ 1141 Brackets Sequence 括号匹配 区间DP

题目描述:

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;
}

你可能感兴趣的:(区间DP)