#P1944. 最长括号匹配
最长括号匹配
Description
Given a string consisting of the four brackets (, ), [ and ], find its longest bracket-matching substring. Specifically, a string is bracket-matching if it satisfies the following conditions:
()and[]are bracket-matching strings.- If A is a bracket-matching string, then (A) and [A] are bracket-matching strings.
- If A and B are both bracket-matching strings, then AB is also a bracket-matching string.
For example: ()、[]、([])、()() are bracket-matching strings, while ][ and [(]) are not.
A substring of string is a string formed by a consecutive block of characters from .
For example, A、B、C、ABC、CAB、ABCABC are all substrings of the string ABCABC, while D、BA、ACB are not. The empty string is a substring of any string.
Input Format
Input consists of one line, a non-empty string composed only of ()[].
Output Format
Output one line: the longest bracket-matching substring. If there are multiple substrings with the same length, output the one that appears earlier in the original string.
([(][()]]()
[()]
())[]
()
Hint
Constraints
Let be the length of the input string.
For of the testdata, .
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号