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

Posted yzm10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)相关的知识,希望对你有一定的参考价值。

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循环)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #244 (Div. 2) D. Match & Catch 后缀数组

Match & Catch CodeForces - 427D 后缀自动机水题

try-catch语句

为啥 try..catch..finally 块的 finally 节在 catch 之前运行?

在java语言中try-catch的catch中能否再加try--catch?

try/catch 和 MFC TRY/CATCH 有啥区别?