poj 2955 Brackets(区间dp)

本博客探讨了一个仅包含括号字符的字符串中,寻找最长合法括号匹配子序列的问题。通过动态规划方法,实现高效求解并提供代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

Brackets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3470 Accepted: 1792

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 a1a2an, 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, ai1ai2aim 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

Source


题意:给一个只包含括号的串,问该串的最长的合法括号匹配的子序列的长度?

题解:先考虑第一个括号,有两种操作:删除这个括号;将该括号与另一个括号匹配。

用dp[l][r]表示区间[l,r]的最长合法子序列的长度,转移就是:

1,删除第一个括号

dp[l][r]=dp[l+1][r]

2,将第一个括号和第i个括号匹配

dp[l][r]=min(dp[l+1][i-1]+dp[i+1][r]+2);

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
#define nn 110
#define inff 0x7fffffff
#define eps 1e-8
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n;
char s[nn];
int dp[nn][nn];
int lj[nn][nn];
bool check(int l,int r)
{
    if(s[l]=='('&&s[r]==')')
        return true;
    if(s[l]=='['&&s[r]==']')
        return true;
    return false;
}
int dfs(int l,int r)
{
    if(dp[l][r]!=-1)
        return dp[l][r];
    if(l>r)
        return 0;
    if(l==r)
        return dp[l][r]=0;
    dp[l][r]=0;
    int i;
    for(i=l+1;i<=r;i++)
    {
        if(check(l,i))
            dp[l][r]=max(dp[l][r],dfs(l+1,i-1)+dfs(i+1,r)+2);
    }
    dp[l][r]=max(dp[l][r],dfs(l+1,r));
    return dp[l][r];
}
int main()
{
    int i;
    while(scanf("%s",s)!=EOF)
    {
        if(strcmp(s,"end")==0)
            break;
        int len=strlen(s);
        memset(dp,-1,sizeof(dp));
        printf("%d\n",dfs(0,len-1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值