• Brackets(括号最大匹配问题(区间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
    ",dp[0][n-1]);
            }
        }
    
        return 0;
    }
  • 相关阅读:
    atitit查询表修改表字段没反应--解锁锁定的表
    atitit.自适应设计悬浮图片的大小and 位置
    .net 科学类型相关问题
    js eval()执行传参函数的写法
    oracle里如何将两个日期的时间差返回**时**分的格式
    .NET开源项目介绍及资源推荐:数据持久层
    highCharts 电流表、电压表
    win7 telnet命令无法使用
    ascx aspx ashx asmx 文件的作用
    Oracle 新建序列值
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10822598.html
Copyright © 2020-2023  润新知