集训第五周动态规划 J题 括号匹配

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 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)表示区间ij内的正确括号个数

dp(i,j)=max{dp(i+1,j-1)+2 (仅当外层括号匹配),dp(i,k)+dp(k,j)(进行内层括号检查)}

#include"iostream"
#include"cstring"
using namespace std;

int dp[110][110];
string a;

void Work()
{
    int len=a.length();
    //cout<<len<<endl;
    memset(dp,0,sizeof(dp));
    int ans=0;
    for(int i=0;i<len;i++)
    {
        for(int j=0,k=i;k<len;k++,j++)
        {
            if((a[j]==‘(‘&&a[k]==‘)‘)||(a[j]==‘[‘&&a[k]==‘]‘))
            dp[j][k]=dp[j+1][k-1]+2;
            for(int t=j+1;t<k;t++)
            if(dp[j][t]+dp[t][k]>dp[j][k])
            dp[j][k]=dp[j][t]+dp[t][k];
            if(dp[j][k]>ans) ans=dp[j][k];
        }
    }
    cout<<ans<<endl;
}

int main()
{
    while(cin>>a)
    {
     if(a=="end") break;
     Work();
    }
    return 0;
}

O(O_O)O

时间: 2024-08-07 16:37:30

集训第五周动态规划 J题 括号匹配的相关文章

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

集训第五周动态规划 I题 记忆化搜索

Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.在上面的例子

集训第五周动态规划 G题 回文串

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

集训第五周动态规划 F题 最大子矩阵和

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this probl

集训第五周动态规划 K题 背包

Description Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of a

2018暑假集训第五周感想

第五周有点漫长..题打得有点磨,急躁,自卑等等负面情绪不断出来(ㄒoㄒ) 线段树真难,dp也真难..如果线段树是有思路实现不了,dp就是完全没思路,核心思想就是找一个转移方程,然而ヽ(´¬`)ノ 寻找dp的转移方程真是一个艰难的过程,同时还伴随着恐怖的状态压缩,也没有什么固定的套路和方法,只能靠多练习和领悟了(?•ω•?) dp也就是动态规划是针对一类最优解的算法,核心思想是类似分治,把一个问题分解成若干个子问题,通过每一个子问题的最优决策得到最优解(- ̄▽ ̄)- dp的实现有递推,也有记忆化搜

集训第五周D题 LCS

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa

程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8. ijAl- 9. ijAl-mnop 10. qrst-abcd- 11. abcd-qrst-abcd- uvw xyz about big me take abcd qrst-abcd- 要 求:MyString类必须是从C++的标准类string类派生而来.提示1:如果将程序中所有 "My

第十五周oj刷题——Problem I: C++ 习题 比较大小-类模板

Description 声明一个类模板,利用它分别实现两个整数.浮点数和字符的比较,求出大数和小数.说明:在类模板外定义各成员函数. Input 输入两个整数.两个浮点数和两个字符 Output 从大到小输出两个整数.两个浮点数和两个字符 Sample Input 3 7 45.78 93.6 a A Sample Output 7 3 93.60 45.78 a A   /* All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年