Description
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、只有一种括号的时候,可以用栈来存储:当栈不为空,且下一个符号为右半部分,此时就出栈。
2、有两种括号的话,如果是平衡的也可以用栈来做。但是如果不是平衡的就不成立,比如:([]]) 实际上是有4个符号是匹配的,但是如果用栈做的话,只会输出2。
3、从题目中的长度最大为100可以看出,这道题是用区间dp来做的,复杂度为 O ( n 3 ) O(n^3) O(n3