#C5211. Longest Balanced Brackets
Longest Balanced Brackets
Longest Balanced Brackets
Given a string composed solely of the characters '(' and ')', determine the length of the longest contiguous substring that forms a balanced bracket sequence.
A sequence is considered balanced if every opening bracket \( ( \) is matched with a corresponding closing bracket \( ) \) in the correct order. That is, the substring is balanced if it can be fully reduced by the matching process.
Note: You should read the input from stdin
and print your answer to stdout
.
The problem can be mathematically stated as: find the maximum length \( L \) such that there exists an index interval \([i, j]\) which satisfies the condition that the number of '(' equals the number of ')' and every prefix of the substring has at least as many '(' as ')'.
inputFormat
The input consists of a single line containing a string \( S \) consisting only of '(' and ')'. The length of \( S \) does not exceed 100,000 characters.
outputFormat
Output a single integer indicating the length of the longest contiguous balanced (well-formed) substring found in \( S \).
## sample(()(
2