zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    关于EasyUI datagrid 无法在dialog中显示的问题分析及解决方案!
    WPF 矩形框8个控制点伸缩及拖拽
    Socket异步通信及心跳包同时响应逻辑分析(最后附Demo)。
    C#断点续传下载。
    C# 全屏坐标及区域坐标获取。自定义光标及系统光标描边捕捉显示。
    解决项目无法添加VBIDE问题
    python爬虫-入门-了解爬虫
    字符串输入数字
    面试题3--数组中的重复数字(new数组的新写法)
    等号操作符重载为什么不能用友元函数大揭秘,以及函数没有等到重载的时候赋值会出现什么现象(盲点)
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107229.html
Copyright © 2011-2022 走看看