CF1726C.Jatayu's Balanced Bracket Sequence

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Last summer, Feluda gifted Lalmohan-Babu a balanced bracket sequence ss of length 2n2 n .

Topshe was bored during his summer vacations, and hence he decided to draw an undirected graph of 2n2 n vertices using the balanced bracket sequence ss . For any two distinct vertices ii and jj ( 1i<j2n1 \le i < j \le 2 n ), Topshe draws an edge (undirected and unweighted) between these two nodes if and only if the subsegment s[ij]s[i \ldots j] forms a balanced bracket sequence.

Determine the number of connected components in Topshe's graph.

See the Notes section for definitions of the underlined terms.

输入格式

Each test contains multiple test cases. The first line contains a single integer tt ( 1t1051 \le t \le 10^5 ) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn ( 1n1051 \le n \le 10^5 ) — the number of opening brackets in string ss .

The second line of each test case contains a string ss of length 2n2 n — a balanced bracket sequence consisting of nn opening brackets "(", and nn closing brackets ")".

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

输出格式

For each test case, output a single integer — the number of connected components in Topshe's graph.

输入输出样例

  • 输入#1

    4
    1
    ()
    3
    ()(())
    3
    ((()))
    4
    (())(())

    输出#1

    1
    2
    3
    3

说明/提示

Sample explanation:

In the first test case, the graph constructed from the bracket sequence (), is just a graph containing nodes 11 and 22 connected by a single edge.

In the second test case, the graph constructed from the bracket sequence ()(()) would be the following (containing two connected components):

Definition of Underlined Terms:

  • A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters ++ and 11 . For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.
  • The subsegment s[lr]s[l \ldots r] denotes the sequence [sl,sl+1,,sr][s_l, s_{l + 1}, \ldots, s_r] .
  • A connected component is a set of vertices XX such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to XX violates this rule.
首页