CF2196D.Double Bracket Sequence

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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

We call a string tt 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 ss into any beautiful string; for this, you can change the characters: in one operation, you can choose a position ii, such that 1≤i≤n1 \le i \le n and change the character sis_{i} to any round or square bracket.

What is the minimum number of operations required to transform ss 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.

给定一个长度为偶数的字符串 ss,由字符 "("、")"、"[" 和 "]" 组成,它们是两种类型的括号(圆括号和方括号)。

我们称一个字符串 tt 是优美的,当且仅当它同时满足以下两个条件:

  • 所有圆括号构成的子序列是一个合法括号序列∗^{\text{∗}};
  • 所有方括号构成的子序列是一个合法括号序列。

例如,字符串 "[(][)[]]()" 是优美的:其中所有圆括号构成的子序列 "()()" 是一个合法括号序列,而所有方括号构成的子序列 "[][[]]" 也是一个合法括号序列。

你希望将 ss 变为某个优美的字符串;为此,你可以执行若干次修改操作:每次操作中,你可以选择一个位置 ii(满足 1≤i≤n1 \le i \le n),并将 sis_i 修改为任意一个圆括号或方括号。

问:将 ss 变为某个优美字符串所需的最少操作次数是多少?

∗^{\text{∗}}一个括号序列被称为合法的,是指可以在其中插入符号 "+" 和 "1",从而得到一个合法的数学表达式。例如,序列 "(())()"、"[]" 和 "(()(()))" 是合法的,而 ")("、"[[]" 和 "(()))(" 则不是。

输入格式

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

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

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

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5})—— 字符串 ss 的长度。保证 nn 为偶数。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,其字符仅由 "("、")"、"[" 和 "]" 组成。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——即该问题的答案。

输入输出样例

  • 输入#1

    5
    2
    [)
    4
    [)[(
    4
    ))[[
    4
    ([)]
    6
    [)]](]

    输出#1

    1
    2
    2
    0
    2

说明/提示

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

In the second test case, from ss, in 22 operations, you can obtain "[][]", by changing the characters at positions 22 and 44.

In the third test case, from ss, in 22 operations, you can obtain "()[]", by changing the characters at positions 11 and 44.

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

在第一个测试用例中,从字符串 ss 出发,通过修改第二个字符,可以得到 "[]"。

在第二个测试用例中,从字符串 ss 出发,经过 22 次操作,可以通过修改位置 22 和 44 处的字符,得到 "[][]"。

在第三个测试用例中,从字符串 ss 出发,经过 22 次操作,可以通过修改位置 11 和 44 处的字符,得到 "()[]"。

在第四个测试用例中,ss 本身已是优美的,因为它可以被划分为子序列 "()" 和 "[]"。

输入解题思路,AI测评打分。不知道怎么写?

首页