Problem Description
Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced:
+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.
Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) -- the number of strings.
Each of the next n lines contains a string s

这道题目要求对包含'('和')'的字符串进行重新排序,然后连接成新的字符串,寻找最长的平衡子序列(不一定连续)。通过预处理括号,将问题简化为处理"((("、")))"或")))(((("的情况。采用贪心策略,当左括号少于右括号时按左括号升序排列,否则按左括号降序排列,以最大化平衡子序列长度。
最低0.47元/天 解锁文章
493

被折叠的 条评论
为什么被折叠?



