cswork / OA打开练习工作台

OracleOA #2

Balancing Parentheses

估计中等可在线判题

考点(AI 标注)单调栈与堆字符串

Only insertions of parentheses are allowed. Find the minimum number of insertions needed to turn the original string into a valid parenthesis string.

输入格式

One line containing a string of only ( and ), with full original bounds 1≤length≤100000.

输出格式

Output a single integer, the answer.

样例 1

输入:

()))

输出:

2

样例 2

输入:

))((

输出:

4

样例 3

输入:

()()

输出:

0

样例说明

The three examples need 2, 4, and 0 insertions respectively. The second can become (())(()); the source's (()))(()) is wrong in both length and validity and has been corrected.