W2 - Catch Overflow!

题目描述

Gugug gives you a function f written in a basic programming language. The function accepts an integer value x. x is an integer variable and can be assigned values from 0 to 2^{32}−1. The function contains three types of commands:
for n — for loop;
end — every command between “for n” and corresponding “end” is executed n times;
add — adds 1 to x.
After the execution of these commands, value of x is returned.
Every “for n” is matched with “end”, thus the function is guaranteed to be valid. “for n” can be immediately followed by “end”.“add” command can be outside of any for loops.
Notice that “add” commands might overflow the value of x! It means that the value of x becomes greater than 2^{32}−1 after some “add” command.
Now you run f(0) and wonder if the resulting value of x is correct or some overflow made it incorrect.
If overflow happened then output “OVERFLOW!!!”, otherwise print the resulting value of x.

Input

The first line contains a single integer l (1 <= l <= 10^5) — the number of lines in the function.
Each of the next l lines contains a single command of one of three types:
for n (1 <= n <= 100) — for loop;
end — every command between “for n” and corresponding “end” is executed n times;
add — adds 1 to x.

Output

If overflow happened during execution of f(0), then output “OVERFLOW!!!”, otherwise print the resulting value of x.

Examples

Input
9
add
for 43
end
for 10
for 15
add
end
add
end
Output
161
Input
2
for 62
end
Output
0
Input
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
Output
OVERFLOW!!!

Note

In the first example the first “add” is executed 1 time, the second “add” is executed 150 times and the last “add” is executed 10 times. Note that “for n” can be immediately followed by “end” and that “add” can be outside of any for loops.
In the second example there are no commands “add”, thus the returning value is 0.
In the third example “add” command is executed too many times, which causes x to go over
2^{32}-1.

分析

这道题的思路还是很简单的,主要问题就是当溢出时如何处理,我第一次提交时简单粗暴的规定,只要栈顶的数超过范围直接判定为NO,显然这是不恰当的,栈顶的数据超限不一定就在这层循环内执行add操作,所以,这时不能直接判错退出,需要继续执行。我接下来的思路就是当记录和的量超过范围就让一个标志变量设置为true,以后的指令只是输入而不起作用,然而这样也造成了未知错误。参考了大佬的思路,当超限时,简单的将那个最大值进栈,而不是已经超限的值。这样经判断long long能存储值最大的情况,最后判断和是否大于范围就可以了。

Code

/*
 * @Description: Catch Overflow
 * @version:
 * @Author:
 * @Date: 2021-04-06 20:20:18
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-07 17:27:51
 */
// 来源于网络
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
const long long INF = 1ll << 32;
stack<long long> sta;
char op[5];
int main(void)
{
    unsigned long long l, ans = 0;
    cin >> l;
    sta.push(1);
    for (int i = 0; i < l; i++)
    {
        int x;
        cin >> op;
        if (op[0] == 'a')
            ans += sta.top();
        else if (op[0] == 'f')
        {
            cin >> x;
            // NOTE:如果超过范围就让2^32进栈,根据分析,最多100000条指令,最多是50000个for,则最大就是进行50000*100*2^32次方,计算得,小于unsigned long long可以表示的范围
            sta.push(min(INF,sta.top() * x));
        }
        else
            sta.pop();
    }
    if (ans < INF)
        cout << ans << endl;
    else
        cout << "OVERFLOW!!!" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian圣楠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值