logo AlgoBeat OnlineJudge
登录 注册

#217359. [ICPC 2026 Xi'an I] Palindromic and Balanced

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Yuki discovered that a palindromic bracket sequence cannot be a balanced bracket sequence, so she designed another way to define a palindromic balanced bracket sequence.

Yuki defines a according to the following rules:

  • The empty string is a palindromic bracket sequence.
  • and are palindromic bracket sequences.
  • If a bracket sequence is a palindromic bracket sequence, then and are palindromic bracket sequences.

Yuki defines a according to the following rules:

  • The empty string is a balanced bracket sequence.
  • If a bracket sequence is a balanced bracket sequence, then is a balanced bracket sequence.
  • If bracket sequences and are both balanced bracket sequences, then (the concatenation of the two bracket sequences) is a balanced bracket sequence.

For a bracket sequence , Yuki defines as a if and only if:

- is a palindromic bracket sequence.

  • is a balanced bracket sequence.

Specifically, the empty string and are also palindromic balanced bracket sequences.

For example, and are palindromic balanced bracket sequences, while and are not.

Now, Yuki has a bracket sequence of length , and she wants to find the longest subsequence of that is a palindromic balanced bracket sequence. However, Yuki does not know how to do this, so you need to help her find the length of the longest such subsequence.

: A sequence is a subsequence of sequence if and only if can be obtained by deleting zero or more elements from ; specifically, the empty sequence is a subsequence of any sequence.

输入格式

This problem contains multiple test cases.

The first line contains a positive integer , representing the number of test cases.

For each test case:

  • The first line contains a positive integer .
  • The second line contains a bracket sequence of length .

It is guaranteed that the sum of over all test cases does not exceed .

输出格式

For each test case, output a single line containing an integer representing the length of the longest subsequence that satisfies the condition.

样例

样例输入 1

3
5
(()((
7
)))((((
8
())(()()

样例输出 1

2
0
6

数据范围与提示

For the first test case:

  • The longest subsequences satisfying the condition are and , so the answer is .

For the second test case:

  • The longest subsequence satisfying the condition is the empty string, so the answer is .

For the third test case:

  • The longest subsequence satisfying the condition is , so the answer is .