标签: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 232−1232−1. The function contains three types of commands:
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:
Output
If overflow happened during execution of f(0)f(0), then output "OVERFLOW!!!", otherwise print the resulting value of xx.
Examples
9 add for 43 end for 10 for 15 add end add end
161
2 for 62 end
0
11 for 100 for 100 for 100 for 100 for 100 add end end end end end
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.
题意:
给出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