A15288 | Jatayu's Balanced Bracket Sequence
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Last summer, Feluda gifted Lalmohan-Babu a balanced bracket sequence $s$ of length $2 n$ .
Topshe was bored during his summer vacations, and hence he decided to draw an undirected graph of $2 n$ vertices using the balanced bracket sequence $s$ . For any two distinct vertices $i$ and $j$ ( $1 \le i < j \le 2 n$ ), Topshe draws an edge (undirected and unweighted) between these two nodes if and only if the subsegment $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.
Topshe was bored during his summer vacations, and hence he decided to draw an undirected graph of $2 n$ vertices using the balanced bracket sequence $s$ . For any two distinct vertices $i$ and $j$ ( $1 \le i < j \le 2 n$ ), Topshe draws an edge (undirected and unweighted) between these two nodes if and only if the subsegment $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 $t$ ( $1 \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 $n$ ( $1 \le n \le 10^5$ ) — the number of opening brackets in string $s$ .
The second line of each test case contains a string $s$ of length $2 n$ — a balanced bracket sequence consisting of $n$ opening brackets "(", and $n$ closing brackets ")".
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the number of opening brackets in string $s$ .
The second line of each test case contains a string $s$ of length $2 n$ — a balanced bracket sequence consisting of $n$ opening brackets "(", and $n$ closing brackets ")".
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^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 $1$ and $2$ 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 $1$ . For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.
- The subsegment $s[l \ldots r]$ denotes the sequence $[s_l, s_{l + 1}, \ldots, s_r]$ .
- A connected component is a set of vertices $X$ 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 $X$ violates this rule.
In the first test case, the graph constructed from the bracket sequence (), is just a graph containing nodes $1$ and $2$ 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 $1$ . For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.
- The subsegment $s[l \ldots r]$ denotes the sequence $[s_l, s_{l + 1}, \ldots, s_r]$ .
- A connected component is a set of vertices $X$ 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 $X$ violates this rule.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted