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

    ID: 17187 Type: RemoteJudge 2000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>区间 DPICPC2026省赛/邀请赛西安

[ICPC 2026 Xi'an I] Palindromic and Balanced

题目描述

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 palindromic bracket sequence\textbf{palindromic bracket sequence} according to the following rules:

  • The empty string is a palindromic bracket sequence.
  • (\texttt( and )\texttt) are palindromic bracket sequences.
  • If a bracket sequence ss is a palindromic bracket sequence, then (s(\texttt( s \texttt( and )s)\texttt) s \texttt) are palindromic bracket sequences.

Yuki defines a balanced bracket sequence\textbf{balanced bracket sequence} according to the following rules:

  • The empty string is a balanced bracket sequence.
  • If a bracket sequence ss is a balanced bracket sequence, then (s)\texttt{(}s\texttt{)} is a balanced bracket sequence.
  • If bracket sequences ss and tt are both balanced bracket sequences, then stst (the concatenation of the two bracket sequences) is a balanced bracket sequence.

For a bracket sequence s=s1…sns = s_1 \dots s_n, Yuki defines ss as a palindromic balanced bracket sequence\textbf{palindromic balanced bracket sequence} if and only if:

-s2…sn−1s_2 \dots s_{n-1} is a palindromic bracket sequence.

  • s1…sns_1 \dots s_n is a balanced bracket sequence.

Specifically, the empty string and ()\texttt{()} are also palindromic balanced bracket sequences.

For example, (())()\texttt{(())()} and ()()(()())\texttt{()()(()())} are palindromic balanced bracket sequences, while ((()))\texttt{((()))} and ()()(())\texttt{()()(())} are not.

Now, Yuki has a bracket sequence ss of length nn, and she wants to find the longest subsequence∗^\ast of ss 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.

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

输入格式

This problem contains multiple test cases.

The first line contains a positive integer tt (1≤t≤5000)(1 \le t \le 5000), representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn (1≤n≤5000)(1 \le n \le 5000).
  • The second line contains a bracket sequence ss of length nn (si∈{(,)})(s_i \in \{\texttt(,\texttt)\}).

It is guaranteed that the sum of nn over all test cases does not exceed 10410^4.

输出格式

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

3
5
(()((
7
)))((((
8
())(()()
2
0
6

提示

For the first test case:

  • The longest subsequences satisfying the condition are s1s3=()s_1s_3 = \texttt{()} and s2s3=()s_2s_3 = \texttt{()}, so the answer is 22.

For the second test case:

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

For the third test case:

  • The longest subsequence satisfying the condition is s1s2s4s5s6s8=()(())s_1s_2s_4s_5s_6s_8 = \texttt{()(())}, so the answer is 66.