A14480. Higher Order Functions
编程题
普及/提高-
知识点
题目描述
Helen studies functional programming and she is fascinated with a concept of higher order functions — functions that are taking other functions as parameters. She decides to generalize the concept of the function order and to test it on some examples.
For her study, she defines a simple grammar of types. In her grammar, a type non-terminal $T$ is defined as one of the following grammar productions, together with $\textrm{order}(T)$ , defining an order of the corresponding type:
- "()" is a unit type, $\textrm{order}(\textrm{"}\texttt{()}\textrm{"}) = 0$ .
- "(" $T$ ")" is a parenthesized type, $\textrm{order}(\textrm{"}\texttt{(}\textrm{"}\,T\,\textrm{"}\texttt{)}\textrm{"}) = \textrm{order}(T)$ .
- $T_1$ "->" $T_2$ is a functional type, $\textrm{order}(T_1\,\textrm{"}\texttt{->}\textrm{"}\,T_2) = max(\textrm{order}(T_1) + 1, \textrm{order}(T_2))$ . The function constructor $T_1$ "->" $T_2$ is right-to-left associative, so the type "()->()->()" is the same as the type "()->(()->())" of a function returning a function, and it has an order of $1$ . While "(()->())->()" is a function that has an order-1 type "(()->())" as a parameter, and it has an order of $2$ .
Helen asks for your help in writing a program that computes an order of the given type.
For her study, she defines a simple grammar of types. In her grammar, a type non-terminal $T$ is defined as one of the following grammar productions, together with $\textrm{order}(T)$ , defining an order of the corresponding type:
- "()" is a unit type, $\textrm{order}(\textrm{"}\texttt{()}\textrm{"}) = 0$ .
- "(" $T$ ")" is a parenthesized type, $\textrm{order}(\textrm{"}\texttt{(}\textrm{"}\,T\,\textrm{"}\texttt{)}\textrm{"}) = \textrm{order}(T)$ .
- $T_1$ "->" $T_2$ is a functional type, $\textrm{order}(T_1\,\textrm{"}\texttt{->}\textrm{"}\,T_2) = max(\textrm{order}(T_1) + 1, \textrm{order}(T_2))$ . The function constructor $T_1$ "->" $T_2$ is right-to-left associative, so the type "()->()->()" is the same as the type "()->(()->())" of a function returning a function, and it has an order of $1$ . While "(()->())->()" is a function that has an order-1 type "(()->())" as a parameter, and it has an order of $2$ .
Helen asks for your help in writing a program that computes an order of the given type.
输入格式
The single line of the input contains a string consisting of characters '(', ')', '-', and '>' that describes a type that is valid according to the grammar from the problem statement. The length of the line is at most $10^4$ characters.
输出格式
Print a single integer — the order of the given type.
输入输出样例
输入 #1
()
输出 #1
0
输入 #2
()->()
输出 #2
1
输入 #3
()->()->()
输出 #3
1
输入 #4
(()->())->()
输出 #4
2
输入 #5
()->(((()->())->()->())->())
输出 #5
3