C. Move Brackets

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a bracket sequence ss of length nn, where nn is even (divisible by two). The string ss consists of n2n2 opening brackets '(' and n2n2 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.

Input

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

The first line of the test case contains one integer nn (2≤n≤502≤n≤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 n2n2 opening and n2n2 closing brackets.

Output

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.

Example

input

Copy

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

output

Copy

1
0
1
3

Note

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 "((()))(())".

 

解題說明:此題其實就是括號匹配,對括號進行遍歷,確保先出現(,否則就需要移動,記錄下移動的個數。

#include<stdio.h>

int main() 
{
	int t;
	char c;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		int a = 0; int b = 0;
		scanf("%d", &n);
		char arr[55];
		scanf("%s", arr);
		for (int i = 0; i<n; i++) 
		{
			if (arr[i] == '(')
			{
				a++;
			}
			if (arr[i] == ')')
			{
				a--;
			}
			if (a<0) 
			{
				b++; 
				a = 0;
			}
		}
		printf("%d \n", b);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章