Bracket sequences
Practice
2 (337 votes)
1 D array
Arrays
Data structures
Problem
91% Success 42469 Attempts 10 Points 3s Time Limit 256MB Memory 1024 KB Max Code

A bracket sequence is a string that contains only characters '(' and ')'.

A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences '()()' and '(())' are correct. The resulting expressions of these sequences are: '(1)+(1)' and '((1+1)+1)'. However, '(', ')(', and '(' are incorrect bracket sequences. 

You are given a bracket sequence \(s(s_1 s_2 ... s_n)\), where \(s_i\) denotes the type of \(i\)'s bracket (open or close). It is not mandatory that \(s\) is necessarily correct. Your task is to determine the number of \(i\)'s such that \(s_i s_{i+1} ... s_n s_1 s_2 ... s_{i-1}\) is a correct bracket sequence.

Input format

The single line contains sequence \(s\).

Output format 

Print the number of shifts denoting the correct bracket sequence.

Constraints

\(|s| \le 5\times 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
25 votes
Tags:
ArraysData Structures1-D
Points:10
869 votes
Tags:
Ad-HocApprovedData StructuresEasyOne-dimensional
Points:10
113 votes
Tags:
ArraysData Structures1-DC++