QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#6444. 括号碎片

Statistics

你正在教授一门编程课,想要讲解平衡括号的概念。你有一个很好的教具,上面写着一个非常长的平衡括号字符串。然而,不幸的是,你的教具被弄碎成了若干片段,而且有些片段可能丢失了!你必须尽力将它们拼凑起来。给定每个片段上的括号字符串,通过以某种顺序连接其中的一些片段,你能构成的最长平衡括号字符串的长度是多少?每个片段最多只能使用一次,且片段不能翻转。

平衡括号字符串定义如下:

  1. 空字符串是平衡括号字符串。
  2. 如果 $A$ 和 $B$ 都是平衡括号字符串,则 $AB$ 也是平衡括号字符串。
  3. 如果 $A$ 是平衡括号字符串,则 $(A)$ 也是平衡括号字符串。

输入格式

每个输入包含一个测试用例。注意,你的程序可能会在不同的输入上运行多次。输入的第一行包含一个整数 $n$ ($1 \le n \le 300$),表示片段的数量。

接下来的 $n$ 行,每行包含一个字符串 $s$ ($1 \le |s| \le 300$),仅由字符 '(' 和 ')' 组成。这描述了其中一个片段。

输出格式

输出一个整数,表示你可以从这些片段中构成的最长平衡括号字符串的长度。注意,空字符串在技术上也是平衡括号字符串,因此总是可以构成长度至少为 0 的字符串(尽管空字符串作为教具效果并不好!)。

样例

样例输入 1

3
())
((()
)()

样例输出 1

10

样例输入 2

5
)))))
)
((
))((
(

样例输出 2

2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.