• POJ:2955-Brackets(经典:括号匹配)


    传送门:http://poj.org/problem?id=2955

    Brackets

    Time Limit: 1000MS Memory Limit: 65536K

    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 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


    1. 这是一个区间dp的经典例题,刚开始的时候还以为可以模拟做,但是事实就是这是不行的,只是自己想的太简单,括号的匹配有很多种。
    2. 区间dp执行的时候,里面先是一段区间一段区间的跑,然后枚举这个区间里面每一种匹配情况,由于在选择区间的时候是从小开始选择,所以在枚举每一种匹配情况的时候都是从前面得到的答案来转移的,这也体现了动态规划,从子问题转移。只不过区间dp里面是从一个小的区间慢慢转移到整个区间。但是怎么处理区间里面的状态要看当前问题的特点。

    #include<stdio.h>
    #include<string.h>
    
    using namespace std;
    const int maxn = 110;
    char s[maxn];
    int dp[maxn][maxn];
    
    bool checke(int S,int E)
    {
        if(s[S] == '(' && s[E] == ')')
            return true;
        if(s[S] == '[' && s[E] == ']')
            return true;
        return false;
    }
    
    int main()
    {
        while(scanf("%s",s))
        {
            if(strcmp(s,"end") == 0)
                break;
            memset(dp,0,sizeof(dp));
            int len = strlen(s);
            for(int i=1;i<len;i++)
                for(int j=0,k=i;k<len;k++,j++)
                {
                    if(checke(j,k))
                        dp[j][k] = dp[j+1][k-1] + 2;
                    for(int z=j;z<k;z++)
                        if(dp[j][z] + dp[z+1][k] > dp[j][k])
                            dp[j][k] = dp[j][z] + dp[z+1][k];
                }
            printf("%d
    ",dp[0][len-1]);
        }
        return 0;
    }
    
  • 相关阅读:
    js试题
    JavaScript 实现removeElement函数
    关于优化
    变量申明的提升,闭包,作用域,this,运算符优先级详细举例及讲解
    前端架构:MVC以及MVVM MVP介绍
    JavaScript事件委托
    各种浏览器的Hack写法(chrome firefox ie等)
    vue页面跳转不刷新时created只执行一次
    iview-admin添加的路由为英文
    vue中使用moment时间戳
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107229.html
Copyright © 2020-2023  润新知