A14480 | Higher Order Functions
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted