zoukankan      html  css  js  c++  java
  • Brackets

    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
    题目大意 :求稳定态的字符有几个。

    题目分析 :动态规划很快也很简单。
    动态量:dp[i][j]表示从i到j的最优的稳定态数。最优子结构。
    动态转移方程:
    1. 外围:(相隔较远的两者可以构成稳态)dp[i][j]=d[i+1][j-1]+1;
    2. 内部: d[i][j]=max(d[i][j],d[i][k]+d[k+1][j]);(i<=k<j 正好可以相连)

    初始化:如果相邻的两个可以构成稳态,即dp[i][j]=1;特殊的dp[i][i]=0;

    #include <stdio.h>
    #include <string.h>
    int f[110][110];
    char s[110];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    int mach(int a,int b)
    {
        if((s[a]=='['&&s[b]==']')||(s[a]=='('&&s[b]==')'))
            return 1;
        return 0;
    }
    int main()
    {
        int i,j,k,len,g;
        while(scanf("%s",s)&&s[0]!='e')
        {
            memset(f,0,sizeof(f));
            len=strlen(s);
            for(i=0;i<len;i++)
            {
                f[i][i]=0;
                if(mach(i,i+1))f[i][i+1]=1;
            }
            for(k=1;k<len;k++)//从0到任意点的最优值。
                  for(i=0;i<len-k;i++)//开始更新
                  {
                      j=i+k;
                      if(mach(i,j))f[i][j]=f[i+1][j-1]+1;
                      for(g=0;g<k;g++)
                         f[i][j]=max(f[i][j],f[i][i+g]+f[i+g+1][j]);
                  }
            printf("%d
    ",f[0][len-1]*2);
        }
        return 0;
    }
    View Code

    还有第二种转移方程:你这么想,在现有的不稳定态中,我们需要添加几个字符才能达到稳态呢?所以我们可以查找不稳定的有多少,再减去这个不稳定态就好了。

    动态量:  dp[i][j]表示从i到j中有不稳态的最优解。

    动态转移:

    1. 外围:(相隔较远的两者可以构成稳态)dp[i][j]=d[i+1][j-1];
    2. 内部: d[i][j]=min(d[i][j],d[i][k]+d[k+1][j]);(i<=k<j 正好可以相连)

     初始化:如果相邻不是稳态d[i][j]=无穷;特殊的dp[i][i]=1;

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int const INF = 0x3fffffff;
    char s[205];
    int dp[205][205];
    
    int main()
    {
        while(scanf("%s", s) != EOF && strcmp(s, "end") != 0)
        {
            int len = strlen(s);
            memset(dp, 0, sizeof(dp));
            for(int i = 0; i < len; i++)
                dp[i][i] = 1;
            for(int l = 1; l < len; l++)
            {
                for(int i = 0; i < len - l; i++)
                {
                    int j = i + l;
                    dp[i][j] = INF;
                    if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']'))
                        dp[i][j] = dp[i + 1][j - 1];
                    for(int k = i; k < j; k++)
                        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
                }
            }
            printf("%d
    ", len - dp[0][len - 1]);
        }
    }
    View Code
  • 相关阅读:
    而立之年的程序员创业者,写给不甘平凡的自己和80、90后!
    无焦虑,不成长!三大方法让你走出焦虑!
    [Chat]实战:仿网易云课堂微信小程序开发核心技术剖析和经验分享
    [干货教程]仿网易云课堂微信小程序开发实战经验
    微信公众号支付之坑:调用支付jsapi缺少参数 timeStamp等错误解决方法
    微信支付之扫码支付开发:我遇到的坑及解决办法(附:Ecshop 微信支付插件)
    OpenCV 4.3 编译和配置
    OpenCV 之 基本绘图
    OpenCV 之 空间滤波
    Qt 地址薄 (二) 添加地址
  • 原文地址:https://www.cnblogs.com/7750-13/p/7381620.html
Copyright © 2011-2022 走看看