Balanced Sequence
Balanced Sequence
Problem Description
Chiaki has nnn strings s1,s2,…,sn" role="presentation" style="position: relative;">s1,s2,…,sns1,s2,…,sns_1,s_2,\dots,s_n consisting of ‘(’ and ‘)’. A string of this type is said to be balanced:
- if it is the empty string
- if AAA and B" role="presentation" style="position: relative;">BBB are balanced, ABABAB is balanced,
- if AAA is balanced, (A)" role="presentation" style="position: relative;">(A)(A)(A) is balanced.
Chiaki can reorder the strings and then concatenate them get a new string ttt. Let f(t)" role="presentation" style="position: relative;">f(t)f(t)f(t) be the length of the longest balanced subsequence (not necessary continuous) of ttt. Chiaki would like to know the maximum value of f(t)" role="presentation" style="position: relative;">f(t)f(t)f(t) for all possible ttt.
Input
There are multiple test cases. The first line of input contains an integer T" role="presentation" style="position: relative;">TTT, indicating the number of test cases. For each test case: The first line contains an integer nnn (1≤n≤105" role="presentation" style="position: relative;">1≤n≤1051≤n≤1051 \le n \le 10^5) – the number of strings. Each of the next nnn lines contains a string si" role="presentation" style="position: relative;">sisis_i (1≤|si|≤1051≤|si|≤1051 \le |s_i| \le 10^5) consisting of (' and
)’. It is guaranteed that the sum of all |si||si||s_i| does not exceeds 5×1065×1065 \times 10^6.
Output
For each test case, output an integer denoting the answer.
Sample Input
2
1
)()(()(
2
)
)(
Sample Output
4
2
题目概述
有n行只包含左右括号的字符串,问如何连接这些字符串可以使得匹配到的括号最多(包括本行自身含有的“()”)
解题思路
我们先算每一行字符串中含有的成对的(),并在该行中删除已配对过的“(” 和 “)”
进行完以上操作我们可以发现每行数组都只剩下多个“)” “(”。且)全在左边,(全在右边。(每个字符串都这么处理后剩下都为“)))((((((”结构的括号串。)
那么我们把剩下的字符串拼接
要想匹配到的括号最多,则尽量让每个括号都起作用了;
如果只包含左括号则把他放在最前边,如果只有右括号把他放在最后边;
可以看出左括号多于右括号的在前,反之在后;
同是左括号多于右括号的,右括号少的在前;
同是右括号多于左括号的,左括号少的在后;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>using namespace std;
const int mmax=1e5+7;
char st[mmax];
struct node
{int l,r;
}a[mmax];bool cmp(node x,node y)
{if (x.r >= x.l && y.r < y.l)return false;if (x.r < x.l && y.r >= y.l)return true; //')'比'('多的放后面 //')'比'('少 比较')'少的放前面 ')'比'('多 比较'('少的放后面if (x.r < x.l && y.r < y.l)return x.r<y.r;else return x.l>y.l;
}
int main()
{int t;scanf("%d",&t);while(t--){int n,sum=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",st);int len=strlen(st);a[i].l=0;a[i].r=0;for(int j=0;j<len;j++){if(st[j]==')'){if(a[i].l<=0)a[i].r++; //没有紧邻的(来配对,( 数量加一else{a[i].l--; //有可以配对的(,消耗一个)。sum++; //配对成功的组数加一}}elsea[i].l++;}}sort(a,a+n,cmp);int l=0; //l表示剩余的“(”数for(int i=0;i<n;i++){if(a[i].r<=l){sum+=a[i].r;l-=a[i].r; //l减少了a[i].r个,用于配对了}else{sum+=l;l=0; //l都用了配对了}l+=a[i].l; //a[i]组字符串右边全是(}printf("%d\n",sum*2);}return 0;
}
Balanced Sequence相关推荐
- 2018 Multi-University Training Contest 1 Balanced Sequence(贪心)
题意: t组测试数据,每组数据有 n 个只由 '(' 和 ')' 构成的括号串. 要求把这 n 个串排序然后组成一个大的括号串,使得能够匹配的括号数最多. 如()()答案能够匹配的括号数是 4,(() ...
- 2018 Multi-University Training Contest 1 部分简单题解析
Preface ACM系列赛第一站,没有进前200还是很伤的. 主要是T2当时没写出来就GG了,后来看了下其实不是很难. 题目按照比赛时我们A的顺序讲,其实我都是被陈潇然大佬和ZWC带飞的. T1 M ...
- 题解报告(CDUT暑期集训——第一场)
题解报告(CDUT暑期集训--第一场) A - Maximum Multiple HDU - 6298 思路:先按照题意打表 发现规律 就出来了(最开始没开long long贡献了3发 然后又忘了换行 ...
- cf819C Jatayu‘s Balanced Bracket Sequence
原题链接 Last summer, Feluda gifted Lalmohan-Babu a balanced bracket sequence s of length 2n. Topshe was ...
- CF1726C Jatayu‘s Balanced Bracket Sequence 题解
题面 题目大意 对于一个长度为 2n2n2n 的合法的括号串 sss,按照如下方法构造一张无向图: 括号序列的所有位置都是无向图中的一个点. 对于该序列的任意位置 lll,它能向另一个位置 rrr 连 ...
- 深度学习基础入门篇[五]:交叉熵损失函数、MSE、CTC损失适用于字识别语音等序列问题、Balanced L1 Loss适用于目标检测
[深度学习入门到进阶]必看系列,含激活函数.优化策略.损失函数.模型调优.归一化算法.卷积模型.序列模型.预训练模型.对抗神经网络等 专栏详细介绍:[深度学习入门到进阶]必看系列,含激活函数.优化策略 ...
- 4759: [Usaco2017 Jan]Balanced Photo
4759: [Usaco2017 Jan]Balanced Photo Time Limit: 10 Sec Memory Limit: 128 MB Submit: 103 Solved: 83 [ ...
- Bi-LSTM-CRF for Sequence Labeling
做了一段时间的Sequence Labeling的工作,发现在NER任务上面,很多论文都采用LSTM-CRFs的结构.CRF在最后一层应用进来可以考虑到概率最大的最优label路径,可以提高指标. 一 ...
- Oracle 12C -- 基于sequence的列的默认值
12C支持先创建一个sequence,然后再将该sequence指定为某个列的值的默认表达式. 和"identity column"具有以下不同点: ·对列的个数没有限制 ·seq ...
最新文章
- OpenJudge/Poj 2001 Shortest Prefixes
- shell中的命令替换和变量替换
- python笔记:fancyimpute
- Vue+Openlayers实现地图缩放图标等比例缩放
- 新浪微博Python客户端接口OAuth2
- 极端情况下收缩 Go 进程的线程数
- java构造函数内部调用_具有内部类构造函数参数的Java Reflection奇数
- 双引擎驱动Quick BI十亿数据0.3秒分析,首屏展示时间缩短30%
- [转载]对称加密DES和TripleDES
- Hibernate 多对多关系实现
- 使用ASP.NET MVC3+EF+Jquery制作文字直播系统(四)——完成篇
- 有关计算机方面的知识竞赛,关于计算机知识竞赛试题
- 史上最全的phpstorm常用配置
- 设计模式之禅——模板方法模式钩子方法
- 微信公众号html教程,公众号排版简易教程
- 在Ubuntu 20.04部署SONIC testbed(topo 0)
- mysql数据库接收不了中文乱码_mysql数据库 中文乱码
- python画图的函数_python画图函数
- 学习笔记 Tianmao 篇 recyclerView 辅助的RecycleAdapterImpl类(适配Sliderview)
- 视频截图获取视频某一帧做图片
热门文章
- 后端开发工程师不懂这些就危险了
- Caused by: org.apache.thrift.TApplicationException: Required field ‘filesAdded‘ is unset
- shell 多个引号冲突_Shell 引号嵌套
- r矢量球坐标系旋度_唯心识学075·如何理解三维直角坐标系中的旋度表达式
- 《图像处理、分析与机器视觉 第四版》 摄像机 相机概述——学习笔记
- 火影手游 所有忍者 奥义 台词
- java.net.MalformedURLException: unknown protocol: jrt 异常解决方法
- 字符串处理,仿古书竖排版
- Terms-level Query之Fuzzy Query
- 2021CCPC网络预选赛(重赛)