【区间dp】括号匹配问题总结

问题一

【POJ2955】

Brackets

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12165 Accepted: 6448

Description

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters ()[, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

<span style="color:#000000">((()))
()()()
([]])
)[)(
([][][)
end</span>

Sample Output

<span style="color:#000000">6
6
4
0
6</span>

Source

Stanford Local 2004

题目大意,给出一段字符串,然后判断最多有多少个括号是匹配的,比如【】或者()都是可以的

思路:

     与合并石子类似,同样通过小的区间内推导大区间的,有地方需要特判的,比如一段区间的两头;

阶段:枚举区间长度

状态:枚举起点位置,

决策:即从k处断开,判断k为何值时最大(最大括号匹配)

状态转移方程:dp【i】【j】=max(dp【i】【j】,dp【i】【k】+dp【k】【j】);

 还有一个判定:if(s[i]=='(‘&&s[j]==’)’||s[i]=='[‘&&s[j]==’]’) 则

                                     dp[i][j]=dp[i+1][j-1]+2;

 代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#define maxn 110
using namespace std;

char s[maxn];
int dp[maxn][maxn];
int main()
{
    while(~scanf("%s",s+1)&&s[1]!='e')
    {
        int n=strlen(s+1);
        memset(dp,0,sizeof(dp));
        for(int len=2;len<=n;len++)
            for(int i=1;i<=n;i++)
        {
            int j=i+len-1;
            if(j>n)
                break;
            if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
            {
                dp[i][j]=dp[i+1][j-1]+2;
            }

            for(int k=i;k<j;k++)
            {
                dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
            }
        }
        cout<<dp[1][n]<<endl;
    }
}

问题二

【POJ】1141Brackets Sequence

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 34302 Accepted: 9920 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

()[()]

Source

Northeastern Europe 2001

题目大意:这个的话,就是给出一段括号,然后让你用最少的步数补好他,

思路:

首先,还是区间的问题,从小区间推到大区间,划分问题区间求解,觉得这个题目,输出是个大问题,所以用pos数组来标记某一段区间的情况,如果某一段区间【i , j】上从k处切断有更小补足的解,就用pos【i】【j】=k的形式来保存,如果区间【i,j】的两端是对称的,即不用更改括号,则领pos为-1即可,

阶段:枚举长度

状态:起点的位置

决策:有关pos数组的保存,以及更小值得查找

状态转移方程:

                       1.      if(dp[i][k]+dp[k+1][j]<dp[i][j])
                                             dp[i][j]=dp[i][k]+dp[k+1][j],pos[i][j]=k; 

                       2.      dp[i][j]=dp[i+1][j-1],pos[i][j]=-1;

代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #define maxn 110
    #define inf 1<<30
    using namespace std;

    int pos[maxn][maxn],dp[maxn][maxn];
    char s[maxn];

    void show(int l,int r)
    {
        if(l>r)
            return ;
        if(l==r)//最后补足就好
        {
            if(s[l]=='('||s[l]==')')
                printf("()");
            else
                printf("[]");
            return ;
        }
        if(pos[l][r]==-1)//对称区间输出两头,递归中间部分
        {
            putchar(s[l]);
            show(l+1,r-1);
            putchar(s[r]);

        }
        else //断点k处分开即可
        {
            show(l,pos[l][r]);
            show(pos[l][r]+1,r);
        }
    }

    int main()
    {
        while(gets(s+1))
        {
            int n=strlen(s+1);
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=n; i++)
                dp[i][i]=1;
            for(int len=1; len<n; len++)
            {
                for(int i=1; i<=n-len; i++)
                {
                    int j=i+len;
                    dp[i][j]=inf; //找的最小值,记得赋值
                    if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
                        dp[i][j]=dp[i+1][j-1],pos[i][j]=-1;//两端对称情况
                    for(int k=i; k<j; k++)
                        if(dp[i][k]+dp[k+1][j]<dp[i][j])//记录k值
                            dp[i][j]=dp[i][k]+dp[k+1][j],pos[i][j]=k;
                }
            }
            show(1,n);
            cout<<endl;
        }
        return 0;
    }

 

    原文作者:括号匹配问题
    原文地址: https://blog.csdn.net/wentong_Xu/article/details/81838351
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞