原题链接

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

Topshe was bored during his summer vacations, and hence he decided to draw an undirected graph of 2n vertices using the balanced bracket sequence s. For any two distinct vertices i and j (1≤i<j≤2n), Topshe draws an edge (undirected and unweighted) between these two nodes if and only if the subsegment s[i…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.

Input
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤105) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤105) — the number of opening brackets in string s.
The second line of each test case contains a string s of length 2n — 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 105.

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

Example
input
4
1
()
3
()(())
3
((()))
4
(())(())
output
1
2
3
3

Note
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…r] denotes the sequence [sl,sl+1,…,sr].
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.

思路:这个题是个假题,不涉及什么算法,找规律就可以了,可惜当时居然没写出来。
Accode:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{ios::sync_with_stdio(false);cin.tie(0);int tt;cin>>tt;while(tt--) {int n;cin>>n;string s;cin>>s;int ans=n+1;for(int i=0; i<2*n-1; i++) {if(s[i]=='('&&s[i+1]==')') {ans--;//每次找到当前点和后继点是一对括号序列答案减一;}}cout<<ans<<'\n';}return 0;
}

cf819C Jatayu‘s Balanced Bracket Sequence相关推荐

  1. CF1726C Jatayu‘s Balanced Bracket Sequence 题解

    题面 题目大意 对于一个长度为 2n2n2n 的合法的括号串 sss,按照如下方法构造一张无向图: 括号序列的所有位置都是无向图中的一个点. 对于该序列的任意位置 lll,它能向另一个位置 rrr 连 ...

  2. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  3. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  4. Codeforces 524F And Yet Another Bracket Sequence 哈希

    And Yet Another Bracket Sequence 枚举起点, 增加的(肯定在最前面, 增加的)肯定在最后面, 比字典序用hash, 卡了自然溢出.. #include<bits/ ...

  5. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  6. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  7. CodeForces - 224C. Bracket Sequence (栈模拟)简单做法

    A bracket sequence is a string, containing only characters "(", ")", "[&quo ...

  8. cf1556Compressed Bracket Sequence

    cf1556Compressed Bracket Sequencex 题意: 给你n个数,奇数位置上的数表示左括号的数量,偶数位置上的数表示右括号的数量.问有多少个[l,r]是满足括号匹配的 题解: ...

  9. LS 24 Bracket sequence(DP)

    Bracket sequence Given string s made up with (, ), ?, count the way to substitude ? with ( or ) to f ...

最新文章

  1. WPF学习总结1:INotifyPropertyChanged接口的作用
  2. android看电脑视频,教你用手机/平板,直接播放电脑上的视频
  3. UVa 11100 旅行2007
  4. moment.js 常用(几天前、相差几天、自然周、自然月)
  5. 我的世界java版移除猪灵了吗_我的世界:激怒僵尸猪灵有奖励,用菌光体堆肥,修复126个漏洞!...
  6. 程序员看不起事业单位员工:一年收入才4万?网友:40岁再比一比,究竟谁更牛逼!!
  7. MySQL基础篇(04):存储过程和视图,用法和特性详解
  8. 编号001!华为超级旗舰入网:获中国首张5G手机“身份证”
  9. power bi 创建空表_如何使用R在Power BI中创建地理地图
  10. stm8L 触摸库使用教程 一步一步
  11. Speedoffice(excel)如何运用公式通过身份证号码提取出性别
  12. elementUi——select选择框的下拉框样式调整——基础积累
  13. 姑娘美丽人体日志博客_60个美丽诱人的博客设计
  14. 红蜘蛛5屏幕测试软件,红蜘蛛5校色仪怎么用?显示器校色及测试色域和色彩精准度详细教程(2)...
  15. journey、voyage、trip、tour、travel的用法区别
  16. kubeadm搭建k8s集群
  17. 好斗or炒作?甲骨文“撕咬”过的那些对手 - 爱上英语题库系统|郭雄飞
  18. HTML5制作一个笑脸
  19. 如何在微信小程序中实现实时会话(聊天)系统
  20. html代码中的nofollow属性

热门文章

  1. 信息奥赛一本通(1058:求一元二次方程)
  2. 家目录漫游 (autofs+NFS)
  3. 中心极限与大数定理律的关系_深入浅出正态分布,大数定律,中心极限定理
  4. 秒转化成天、小时、分钟、秒
  5. android百度开发显示多个点标记,android百度地图显示多个自定义标记
  6. 数据泄漏、删除事件频发,企业应如何构建安全防线?
  7. Linux学习笔记(七)——shell语言
  8. Qt的alignment、margin属性
  9. 渗透测试工程师面试题目大全
  10. 数字验证码的识别(一)