#C1206. Longest Valid Parentheses
Longest Valid Parentheses
Longest Valid Parentheses
Given a string consisting only of the characters '(' and ')', determine the length of the longest valid (well-formed) parentheses substring. A valid substring must adhere to proper pairing rules, meaning every opening parenthesis '(' must have a corresponding closing parenthesis ')' and the pairings must be correctly nested. Mathematically, if we represent '(' as +1 and ')' as -1, a substring ( s[i \ldots j] ) is valid if ( \sum_{k=i}^{j} (s[k]=='(' ? 1 : -1) = 0 ) and the cumulative sum never falls below zero within that range.
You are required to implement a solution that reads input from standard input (stdin) and writes the output to standard output (stdout).
inputFormat
The input consists of a single line containing a string ( s ) composed only of characters '(' and ')'. The length of ( s ) satisfies (1 \leq |s| \leq 10^5).
outputFormat
Output a single integer corresponding to the length of the longest valid (well-formed) parentheses substring found in ( s ).## sample
(()()
4