题目:http://codeforces.com/contest/1175/problem/B

B. Catch Overflow!
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 xxbecomes 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
input

Copy
9
add
for 43
end
for 10
for 15
add
end
add
end
output

Copy
161
input

Copy
2
for 62
end
output

Copy
0
input

Copy
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
output

Copy
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个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!

思路:

最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉

注意:1.如果大于等于inf就要标记溢出

   2.模拟栈如果访问上一个元素则先单独在外++tp,不然本地都是对的但OJ的输出会迷之出错(不知道为什么)

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int main(){
ll inf=,ans=,tmp=;
for(int i=;i<=;i++)inf*=;
ll l,tp=,n,st[amn],have=;
bool overflow=;
string order;
ios::sync_with_stdio();
cin>>l;
st[++tp]=;
while(l--){
cin>>order;
if(order=="add"){
if(!overflow){
ans+=st[tp]; ///加上当前for造成的影响
if(ans>=inf)overflow=; ///溢出标记,是否大于等于inf
}
}
else if(order=="for"){
cin>>n;
tp++;
st[tp]=(min(inf,n*st[tp-])); ///当前这个for造成的影响是当前的n和前面的for的影响,相当于前缀积
}
else
tp--; ///碰到end后消去当前for的影响
}
if(overflow)printf("OVERFLOW!!!\n");
else printf("%lld\n",ans);
}
/**
有l个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!
最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉
**/

[模拟] Codefroces 1175B Catch Overflow!的更多相关文章

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

    You are given a function ff written in some basic language. The function accepts an integer value, w ...

  2. Educational Codeforces Round 66 (Rated for Div. 2) B. Catch Overflow!

    链接:https://codeforces.com/contest/1175/problem/B 题意: You are given a function ff written in some bas ...

  3. CF-1175 B.Catch Overflow!

    题目大意:有一个初始变量,值为0,三种操作 for x 一个循环的开始,循环x次 end 一个循环的结束 add 将变量值加一 问最后变量的值是否超过2^32-1,若超过,输出一串字符,不超过则输出变 ...

  4. C#中try catch中throw ex和throw方式抛出异常有何不同

    我们在C#的try catch代码块中里面经常使用throw语句抛出捕捉到的异常,但是你知道吗使用throw ex和throw抛出捕获到的异常效果是不一样的. 异常捕捉的原理 首先先介绍一下C#异常捕 ...

  5. 程序中try、throw、catch三者之间的关系

    c++程序中,采用一种专门的结构化处理逻辑的异常处理机制. 1.try语句 try语句块的作用是启动异常处理机制,检测try语句块中程序语句执行时可能出现的异常. try语句块总是与catch一同出现 ...

  6. 用Go语言异常机制模拟TryCatch异常捕捉

    有的同学看到Go和TryCatch一起出现,心里可能会说,难道Go语言升级了,加入了try...catch语句.哈哈,其实Go语言从创建之初就没打算加入try...catch语句,因为创建Go的那帮大 ...

  7. 用Go语言异常机制模拟TryCatch异常捕捉1

    有的同学看到Go和TryCatch一起出现,心里可能会说,难道Go语言升级了,加入了try...catch语句.哈哈,其实Go语言从创建之初就没打算加入try...catch语句,因为创建Go的那帮大 ...

  8. ES6 一些笔记

    一. let/const: 1. “暂时性死区”概念:在代码块内,使用let/const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 ...

  9. synchronized锁详解

    synchronized的意义 解决了Java共享内存模型带来的线程安全问题: 如:两个线程对初始值为 0 的静态变量一个做自增,一个做自减,各做 5000 次,结果是 0 吗?(针对这个问题进行分析 ...

随机推荐

  1. Redis:slave flush old data造成实例不可用

    一.问题描述 2019-02-22凌晨02:42分前后,收到集群中 [10.32.52.8:6500] 实例不可用告警,登陆管理界面查看此实例在正常运行状态,期间未出现机器宕机或实例直接挂掉的现象. ...

  2. sms短信服务

    短信服务是app,电商类应用的基础功能.典型场景有: 用户注册,发送验证码 用户找回验证,发送验证码 用户账户异常,发送提示 用户账户变化,通知用户 短信服务开发有几个注意点: 供应商选型 短信模板 ...

  3. 苹果会放弃iPhone吗?

    ​苹果会放弃iPhone吗?一般来讲,这是一个相当白痴的问题,苹果放弃iPhone的概率比唐僧放弃取经的概率要低20倍.前段时间回老家,正在学习英语的小侄子问我:"叔叔,苹果用英语怎么说呀? ...

  4. 悖论当道,模式成空:汽车O2O真是死得其所?

    O2O热潮的兴起似乎来得颇为蹊跷--或许是线上连接线下的模式太过空泛,具有极大的包容性,让各个行业都忍不住在其中横插一脚.在经历过最初的崛起和后来的火爆之后,最终形成目前的寒冬.究其原因,O2O并不是 ...

  5. 「知乎」对中国用户而言,Pure Android 是否比 MIUI 或 Flyme 体验更好? - Donnie的博客

    这篇文章转载自我在知乎上的回答 哎呀-不要站队嘛.其实这是一个很有意思的题目,让我们一点点来看 哦对,谢妖-.本人是Nexus 5用户,系统当然是Pure Android KitKat啦(臭谷粉!点D ...

  6. Daily Practice 2016-09-20

    算法 回文(Palindrome)数字:判断一个数字是不是回文数字,不能使用另外的空间. 提示: 负数可以是回文数字吗? 如果转为字符串需要新分配空间 你也许想到了反转数字,但反转数字可能溢出,怎样处 ...

  7. JavaScript,ajax与springboot之间的传参方式

    Springboot:@PostMapping("/mDelTest")public String mDelTest (@RequestBody List<String> ...

  8. 小白学 Python 数据分析(13):Pandas (十二)数据表拼接

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  9. Go语言中的数据类型转换

    在go语言中,不同类型的变量之间赋值需要显示转换. 语法:T t=T(e) //将i转换为float类型 var j float32=float32(i) 基本数据类型转string 方法1:fmt. ...

  10. 用户HTTP请求过程简单剖析

    用户终端(如电脑浏览器)发起某个url请求,如http://www.baidu.com/1.jpg. 1.电脑首先会对www.baidu.com进行解析请求,获得域名对应的服务器IP. 2.电脑对服务 ...