【括号最大匹配】区间dp

题目

题目
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 ≤ n, ai1ai2 … 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
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6

题目大意

本题大意是给出一组括号序列,空串是合法的,且若串S合法,则(S),[S],SS都是合法的,题目要求我们求出一串中最长的合法子序列长度。

解题思路

通过本题中的最长合法子序列可以想到本题可能用到dp的思路,同时对于这一类连续问题可以想到用区间dp解题。之后关键的便要得到本题的状态转移方程,可以发现对于单独的括号必然不合法,所以每一位的dp[i][i]均为0。之后两位两位寻找,如果构成完整括号,则说明合法即dp[i][i+1]=2,否则为0。当完成上述初始化工作,便可以开始区间dp的关键部分,区间dp与普通dp有所不同的地方还在于他需要遍历区间长度,这里我们从3开始遍历即可。遍历过程中要注意起点加区间长度不要超过总长度,不然会造成RE错误。这里的关键部分如下代码所示:

for(int len = 3; len <= n; len++)
{
    for(int i = 0; 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;
        }
        else 
        {
            dp[i][j] = 0;
		}
        for(int k = i; k < j; k++)
        {
            dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
        } 
    }
}

这里便可以找到离散的子序列中的最大合法长度,需要仔细理解掌握原理方法,同时应对dp类问题一定要注意起始条件、遍历方法和遍历范围等,都是琐碎容易出问题的地方。

具体代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<queue>
#define ll long long
#define MAXN 100005 
using namespace std;

string s;
int dp[105][105];

int main()
{
    while(true)
    {
    	cin >> s;
        if(s == "end")
        {
            break;
        }
        else
        {
            int n = s.size();
            for(int i = 0; i < n; i++)
            {
                dp[i][i] = 0;
            }
            for(int i = 0; i < n-1; i++)
            {
                if((s[i] == '[' && s[i+1] == ']') || (s[i] == '(' && s[i+1] == ')'))
                {
                    dp[i][i+1] = 2;
                }
                else
                {
                    dp[i][i+1] = 0;
                }
            }
            for(int len = 3; len <= n; len++)
            {
                for(int i = 0; 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;
                    }
                    else 
                    {
                    	dp[i][j] = 0;
					}
                    for(int k = i; k < j; k++)
                    {
                        dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
                    } 
                }
            }
            cout << dp[0][n-1] << endl;
        }
    }
    return 0;
}
原创文章 46 获赞 1 访问量 1487

猜你喜欢

转载自blog.csdn.net/weixin_43676449/article/details/106025420
今日推荐