您的位置:首页 > 其它

Brackets

2016-01-26 16:23 429 查看
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 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


Hint

RunIDUser

Problem

All
A - Brackets B - Cheapest Palindrome
C - Brackets Sequence D - Multiplication Puzzle
E - Running F - 炮兵阵地
G - Corn Fields H - Most Powerful
I - String painter J - Cake A
- Brackets
Result

All Accepted Presentation Error
Wrong Answer Time Limit Exceed
Memory Limit Exceed Output Limit Exceed
Runtime Error Compile Error
Unknown Error Submit Error
Queuing && Judging All
Memory

(KB)
Time

(ms)
Language

All C++ C
Java Pascal Python
C# Ruby
Other All
Length

(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: