Codeforces Round 1079 (Div. 1)
D
Double Bracket Sequence
Codeforces
2500
原题
← C2E1 →
翻译中…

Given a string $s$ of even length, consisting of the characters "(", ")", "[" and "]", which are brackets of two types (round and square).

翻译中…

We call a string $t$ beautiful if it satisfies two conditions simultaneously:

翻译中…
  • The subsequence of all round brackets forms a correct bracket sequence$^{\text{∗}}$;
  • The subsequence of all square brackets forms a correct bracket sequence.
翻译中…

For example, the string "[(][)[]]()" is beautiful, as the subsequence of all round brackets in it "()()" forms a correct bracket sequence and the subsequence of all square brackets in it "[][[]]" forms a correct bracket sequence.

翻译中…

You would like to turn $s$ into any beautiful string; for this, you can change the characters: in one operation, you can choose a position $i$, such that $1 \le i \le n$ and change the character $s_{i}$ to any round or square bracket.

翻译中…

What is the minimum number of operations required to transform $s$ into any beautiful string?

翻译中…

$^{\text{∗}}$A bracket sequence is called correct if by inserting the symbols "+" and "1" into it, one can obtain a valid mathematical expression. For example, the sequences "(())()", "[]" and "(()(()))" are correct, while ")(", "[[]" and "(()))(" are not.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^{5}$) — the length of the string $s$. It is guaranteed that $n$ is even.

翻译中…

The second line of each test case contains a string $s$ of length $n$, consisting only of the characters "(", ")", "[" and "]".

翻译中…

It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$

翻译中…

Output

翻译中…

For each test case, output a single integer — the answer to the problem.

翻译中…

Note

翻译中…

In the first test case, from $s$ you can obtain "[]" by changing the second character.

翻译中…

In the second test case, from $s$, in $2$ operations, you can obtain "[][]", by changing the characters at positions $2$ and $4$.

翻译中…

In the third test case, from $s$, in $2$ operations, you can obtain "()[]", by changing the characters at positions $1$ and $4$.

翻译中…

In the fourth test case, $s$ is already beautiful, as it can be divided into the subsequences "()" and "[]".

翻译中…

样例

Input #1
5
2
[)
4
[)[(
4
))[[
4
([)]
6
[)]](]
Output #1
1
2
2
0
2
InputCopy OutputCopy
题目信息
题号
2196D
时间限制
2 s
内存限制
256 MB
标签
数据结构
动态规划
表达式解析
网络流
贪心
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。