QOJ.ac

QOJ

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

#5086. Parentheses Tree

統計

一棵有根有序树 $T$ 可以表示为一个匹配的括号序列 $p(T)$。字符串表示 $p(T)$ 可以递归定义。作为基准情况,仅由单个节点组成的树表示为一对括号 $()$。当一棵有根有序树 $T$ 由一个根节点和 $k$ 棵有序子树 $T_1, T_2, \dots, T_k$ 组成,且这些子树的根节点是该根节点的子节点时,字符串表示 $p(T)$ 定义如下:

$$p(T) := ( + p(T_1) + p(T_2) + \dots + p(T_k) + )$$

在上述表达式中,运算符 $+$ 表示两个字符串的连接。下图展示了两个有根有序树的例子。它们的字符串表示 $p(T_L)$ 和 $p(T_R)$ 分别为 $((()()())())$ 和 $(()((()(()))()))$。

从根节点到叶节点的距离定义为从根节点到达该叶节点所经过的边数。在上图中,根节点用蓝色标出,并标注了从根节点到所有叶节点的距离。对于树 $T_L$ 和 $T_R$,从根节点到所有叶节点的距离之和分别为 7 和 10。

给定一个表示单棵有根有序树的匹配括号序列,编写一个程序,输出从该树的根节点到所有叶节点的距离之和。

输入格式

程序从标准输入读取数据。输入包含一行,为一个表示单棵有根有序树的匹配括号序列。输入不包含除括号以外的任何字符,字符串长度至少为 2,且不超过 $10^7$。

输出格式

程序向标准输出写入数据。输出仅一行,包含从该有根有序树的根节点到所有叶节点的距离之和。

样例

输入 1

((()()())())

输出 1

7

输入 2

(()((()(()))()))

输出 2

10

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.