ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Brackets(括号最大匹配问题(区间dp))

2019-05-06 22:41:39  阅读:244  来源: 互联网

标签:brackets Brackets sequence 括号 int regular str include 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 i1i2, …, 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

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6

代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;
string str;
int dp[105][105];
int main()
{
    while(cin>>str)
    {
        if(str=="end")
        {
            break;
        }
        else
        {
            int n=str.length();
            for(int t=0;t<n;t++)
            {
                dp[t][t]=0;
            }
            for(int t=0;t<n-1;t++)
            {
                if((str[t]=='['&&str[t+1]==']')||(str[t]=='('&&str[t+1]==')'))
                {
                    dp[t][t+1]=2;
                }
                else
                {
                    dp[t][t+1]=0;
                }
            }
            for(int r=3;r<=n;r++)
            {
                for(int i=0;i<n;i++)
                {
                    int j=i+r-1;
                    if(j>n)
                    break;
                    if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[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]);
                    } 
                }
            }
            printf("%d\n",dp[0][n-1]);
        }
    }

    return 0;
}

 

标签:brackets,Brackets,sequence,括号,int,regular,str,include,dp
来源: https://www.cnblogs.com/Staceyacm/p/10822598.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有