码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)

时间:2019-06-30 15:47:49      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:ide   越界   XML   exec   result   add   push   over   pop   

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 2321232−1. The function contains three types of commands:

  • for nn — for loop;
  • end — every command between "for nn" and corresponding "end" is executed nntimes;
  • 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 2321232−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 (1l1051≤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 (1n1001≤n≤100) — for loop;
  • end — every command between "for nn" and corresponding "end" is executed nntimes;
  • 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

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 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 2321232−1.

 

 

题意:

给出for循环伪代码,计算执行add的次数(加了几次)。

思路:

很新颖的一道题,不难想到使用栈模拟。

因为栈底的值会影响栈顶的结果,因此放入累乘值来表示当前的状态,每次add只需加入栈顶的值即可。

具体实现见代码,注意判断越界。

 

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
 
string s;
stack<ll> st;
 
int main()
{
    int t,i;
    ll x=0,y=0;
    scanf("%d",&t);
    st.push(1);
    int f=0;
    while(t--){
        scanf(" ");
        cin>>s;
        if(s[0]==f){
            scanf("%I64d",&x);
            st.push(min(4294967296ll,x*st.top()));
        }
        else if(s[0]==a){
            y+=st.top();
            if(y>=4294967296){
                f=1;
                break;
            }
        }
        else if(s[0]==e){
            st.pop();
        }
    }
    if(f==1) printf("OVERFLOW!!!\n");
    else printf("%I64d\n",y);
    return 0;
}

CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)

标签:ide   越界   XML   exec   result   add   push   over   pop   

原文地址:https://www.cnblogs.com/yzm10/p/11109944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!