Brackets

来源:互联网 发布:中国移动app软件下载 编辑:程序博客网 时间:2024/05/21 08:41

 Brackets
Crawling in process...Crawling failed
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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 a1a2an, your goal is to find the length of the longest regular brackets sequence that is a subsequence ofs. That is, you wish to find the largest m such that for indicesi1, i2, …, im where 1 ≤i1 < i2 < … < imn, ai1ai2aim 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

66406

Hint

RunIDUser
Problem
A - BracketsResult
AllMemory
(KB)Time
(ms)Language
AllLength
(Bytes)Submit Time
Rank
ID
Solve
Penalty
A
B
C
D
E
F
G
H
I
J
Rank
ID
Solve
Penalty
A
B
C
D
E
F
G
H
I
J
1
swust20141567(张晓雨)
2
196
0:43:01
(-1)
2:13:24
 
2
zengjing(swust6349)
1
37
0:37:14
 
3
5120146151(电气1403丁涛)
1
44
0:44:36
 
 
(-4)
4
5120142493(物联1401纪育青)
1
129
2:09:28
 
5
swust_20137007(任柏谦)
1
136
2:16:19
 
6
swust5120146282(物联1401张俊强)
1
168
2:28:30
(-1)
7
5120146273(通信1404李梦慧)
1
319
4:59:03
(-1)
8
5120141477(电子1403张志予)
1
369
6:09:37
 
9
swust_fang(方畋钦)
1
389
6:29:16
 
10
swust5120146267(辜馨月)
1
424
7:04:24
 

10/13
76%
 
0/4
0%
 
1/1
100%
 
11/18
61%

10/13
76%
 
0/4
0%
 
1/1
100%
 
11/18
61%

题意:在输入的一串全是括号的字符串中找到括号的最大的匹配数,也就是寻找()和 [ ] 成对出现的最大组数。

思路:用dp[i][j]保存区间i ~ j当前的最大匹配数,找到状态转移方程后更新。长度为1的区间匹配数为0,长度为2的区间如果两者匹配那么匹配数为2,反之为0;在区间i ~ j,如果i和j匹配,那么有一个状态转移方程:dp[i][j] = dp[i+1][j-1] + 2;还有一种转移,在i ~ j之间枚举断点k,转移方程为dp[i][j] = dp[i][k] + dp[k+1][j],在分成两个区间相加的情况和自身之间取较大的作为当前的最大匹配数。不断的更改区间长度,最后便可得到整个区间的最大匹配数。

代码如下:

#include"cstdio"#include"cstring"#include"iostream"#include"algorithm"using namespace std;char s[105];int dp[105][105];bool is_regular(int x,int y) //判断x,y是否匹配{    if(s[x] == '(' && s[y] == ')')    {        return true;    }    if(s[x] == '[' && s[y] == ']')    {        return true;    }    return false;}int main(){    while(~scanf("%s",s))    {        if(s[0] == 'e')        {            break;        }        int len = strlen(s);        memset(dp,0,sizeof(dp));         for(int i = 0;i < len - 1;i++)        {            if(is_regular(i,i+1)) //区间长度为2,如果匹配则最大匹配数为2            {                dp[i][i+1] = 2;            }        }        for(int k = 3;k <= len;k++) //枚举区间长度        {            for(int i = 0;i+k-1 < len;i++) //起点            {                if(is_regular(i,i+k-1))                {                    dp[i][i+k-1] = dp[i+1][i+k-2] + 2;                }                for(int 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;}




0 0
原创粉丝点击