B. Catch Overflow!

You are given a function ff written in some basic language. The function accepts an integer value, which is immediately written into some variable xx. xx is an integer variable and can be assigned values from 00 to 232−1232−1. The function contains three types of commands:

for nn — for loop;
end — every command between “for nn” and corresponding “end” is executed nn times;
add — adds 1 to xx.
After the execution of these commands, value of xx is returned.

Every “for nn” is matched with “end”, thus the function is guaranteed to be valid. “for nn” can be immediately followed by “end”.“add” command can be outside of any for loops.

Notice that “add” commands might overflow the value of xx! It means that the value of xx becomes greater than 232−1232−1 after some “add” command.

Now you run f(0)f(0) and wonder if the resulting value of xx is correct or some overflow made it incorrect.

If overflow happened then output “OVERFLOW!!!”, otherwise print the resulting value of xx.

Input
The first line contains a single integer ll (1≤l≤1051≤l≤105) — the number of lines in the function.

Each of the next ll lines contains a single command of one of three types:

for nn (1≤n≤1001≤n≤100) — for loop;
end — every command between “for nn” and corresponding “end” is executed nn times;
add — adds 1 to xx.
Output
If overflow happened during execution of f(0)f(0), then output “OVERFLOW!!!”, otherwise print the resulting value of xx.

Examples
inputCopy
9
add
for 43
end
for 10
for 15
add
end
add
end
outputCopy
161
inputCopy
2
for 62
end
outputCopy
0
inputCopy
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
outputCopy
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 nn” 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 xx to go over 232−1232−1.

题目大意:首先的L是给你L个操作,某个操作可能会是add for end add是加1 可以配合for循环使用。每一个for都会对应一个end end就是来终止当前循环的。最大是(1<<32)-1,问你这个数最后是多少,如果大于最大值了,就输出overflow

用栈存储for的嵌套。
比赛的时候第五个测试点一直错,后来看了别人的代码才发现是爆long long 了。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int maxx = 1e5+10;




long long  init()
{
    long long  sum = 1;
    for(int i=0; i<32; i++)
    {
        sum *=2;
    }
    return sum;
}




int main()
{
    int n;
    cin>>n;
    string st;
    stack<long long>s;
    long long  ans = 0;
    s.push(1);
    long long  t = init();
    int i;
    for(i=0; i<n; i++)
    {
        cin>>st;
        if(st=="for")
        {
            long long  x;
            cin>>x;
            s.push(min(t,s.top()*x));        //如果大于t的话直接取t。
        }
        else if(st=="add")
        {
            ans = min(ans+s.top(),t);
        }
        else if(st=="end")
        {
            s.pop();
        }
    }
    if(ans==t)
        cout<<"OVERFLOW!!!"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值