CF1374C.Move Brackets

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a bracket sequence ss of length nn , where nn is even (divisible by two). The string ss consists of n2\frac{n}{2} opening brackets '(' and n2\frac{n}{2} closing brackets ')'.

In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index ii , remove the ii -th character of ss and insert it before or after all remaining characters of ss ).

Your task is to find the minimum number of moves required to obtain regular bracket sequence from ss . It can be proved that the answer always exists under the given constraints.

Recall what the regular bracket sequence is:

  • "()" is regular bracket sequence;
  • if ss is regular bracket sequence then "(" + ss + ")" is regular bracket sequence;
  • if ss and tt are regular bracket sequences then ss + tt is regular bracket sequence.

For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.

You have to answer tt independent test cases.

输入格式

The first line of the input contains one integer tt ( 1t20001 \le t \le 2000 ) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn ( 2n502 \le n \le 50 ) — the length of ss . It is guaranteed that nn is even. The second line of the test case containg the string ss consisting of n2\frac{n}{2} opening and n2\frac{n}{2} closing brackets.

输出格式

For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from ss . It can be proved that the answer always exists under the given constraints.

输入输出样例

  • 输入#1

    4
    2
    )(
    4
    ()()
    8
    ())()()(
    10
    )))((((())

    输出#1

    1
    0
    1
    3

说明/提示

In the first test case of the example, it is sufficient to move the first bracket to the end of the string.

In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.

In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())".

首页