2015 HUAS Summer Trainning #5~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
解题思路:题目的意思是在输入的字符串中寻找有多少对括号匹配,并输出它们的个数。需要注意的是,这个问题不能用顺序解决,因为这是两种括号的匹配问题,会出现交叉情况,之前也用过的队列解决过。

程序代码:

#include <stdio.h> 
#include <string.h> 
#include <algorithm> 
using namespace std;

int max(int x,int y)
{
 if(x>y) return x;
 else return y;
}

int check(char a,char b) 

    if(a==‘(‘ && b==‘)‘) 
        return 1; 
    if(a==‘[‘ && b==‘]‘) 
        return 1; 
    return 0; 

 
int main() 

    char str[105]; 
    int dp[105][105],i,j,k,len; 
    while(scanf("%s",str)) 
    { 
        if(!strcmp(str,"end")) 
            break; 
        len = strlen(str); 
        for(i = 0; i<len; i++) 
        { 
            dp[i][i] = 0;
            if(check(str[i],str[i+1])) 
  dp[i][i+1] = 2;
            else 
   dp[i][i+1] = 0;
        } 
        for(k = 3; k<=len; k++) 
        {
            for(i = 0; i+k-1<len; i++) 
            { 
   dp[i][i+k-1] = 0;
                if(check(str[i],str[i+k-1])) 
     dp[i][i+k-1] = dp[i+1][i+k-2]+2;
                for(j = i; j<i+k-1; j++) 
    
     dp[i][i+k-1] = max(dp[i][i+k-1],dp[i][j]+dp[j+1][i+k-1]);
            } 
        } 
        printf("%d\n",dp[0][len-1]); 
    } 
 
    return 0; 
}

时间: 2024-10-31 09:17:36

2015 HUAS Summer Trainning #5~J的相关文章

2015 HUAS Summer Trainning #6~H

Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000). 接下来共T行,每行输入两个整数L,R(1<= L <= R <= 10

2015 HUAS Summer Trainning #4 B

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so calledscribers. The scriber had been given a book and after several months he finished its copy. One of

2015 HUAS Summer Trainning #4 D

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimalamount of them, such they would completely cover the segment [0, M]. InputThe first line is the number of test cases, followed by a blank line.Eac

2015 HUAS Summer Trainning #4 A

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

2015 HUAS Summer Trainning #5~N

Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … The bone collector had a big bag with a volume of V ,and

2015 HUAS Summer Trainning #5 C

Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missing in 

2015 HUAS Summer Trainning #5 B

Description 一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉. 研究人员有n种类型的砖块,每种类型的砖块都有无限个.第i块砖块的长宽高分别用xi,yi,zi来表示. 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高. 在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一

2015 HUAS Summer Trainning #5~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

2015 HUAS Summer Trainning #4 C

My birthday is coming up and traditionally I’mserving pie. Not just one pie, no, I have a numberN of them, of various tastes and of various sizes. Fof my friends are coming to my party and each ofthem gets a piece of pie. This should be one pieceof o