CF1578H.Higher Order Functions

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 TT is defined as one of the following grammar productions, together with order(T)\textrm{order}(T), defining an order of the corresponding type:

  • "()" is a unit type, order("()")=0\textrm{order}(\textrm{"}\texttt{()}\textrm{"}) = 0.
  • "(" TT ")" is a parenthesized type, order("(" T ")")=order(T)\textrm{order}(\textrm{"}\texttt{(}\textrm{"}\,T\,\textrm{"}\texttt{)}\textrm{"}) = \textrm{order}(T).
  • T1T_1 "->" T2T_2 is a functional type, \textrm{order}(T_1\,\textrm{"}\texttt{- \gt }\textrm{"}\,T_2) = max(\textrm{order}(T_1) + 1, \textrm{order}(T_2)). The function constructor T1T_1 "->" T2T_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 11. While "(()->())->()" is a function that has an order-1 type "(()->())" as a parameter, and it has an order of 22.

Helen asks for your help in writing a program that computes an order of the given type.

海伦正在学习函数式编程,她对高阶函数(即以其他函数作为参数的函数)这一概念非常着迷。她决定将函数“阶数”的概念进行推广,并在一些示例上加以验证。

为开展这项研究,她定义了一种简单的类型文法。在此文法中,类型非终结符 TT 由以下文法规则之一定义,并同时定义其对应类型的阶数 order(T)\textrm{order}(T):

  • "()" 是单位类型,order("()")=0\textrm{order}(\textrm{"}\texttt{()}\textrm{"}) = 0。
  • "( " TT ")" 是加括号的类型,order("(" T ")")=order(T)\textrm{order}(\textrm{"}\texttt{(}\textrm{"}\,T\,\textrm{"}\texttt{)}\textrm{"}) = \textrm{order}(T)。
  • T1T_1 "\->" T2T_2 是函数类型,\textrm{order}(T_1\,\textrm{"}\texttt{- \gt }\textrm{"}\,T_2) = \max(\textrm{order}(T_1) + 1, \textrm{order}(T_2))。函数构造符 T1T_1 "\->" T2T_2 是右结合的,因此类型 "()->()->()" 等价于 "()->(()->())"(即返回一个函数的函数),其阶数为 11;而 "(()->())->()" 则是一个以阶数为 11 的类型 "(()->())" 为参数的函数,其阶数为 22。

海伦请你帮忙编写一个程序,用于计算给定类型的阶数。

输入格式

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 10410^4 characters.

输入仅有一行,包含一个由字符 '('、')'、'-' 和 '>' 组成的字符串,该字符串根据题目描述中的语法规则是一个合法的类型。该行长度至多为 10410^4 个字符。

输出格式

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

输入解题思路,AI测评打分。不知道怎么写?

首页