题目: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. android应用程序源码结构分析

    工程; 1. src文件夹存放源码. 2. gen下有跟src中一样的包文件,内部有一个名为R.java类,它是自动生成的一个类:该目录不用我们开发人员维护, 但又非常重要的目录 . 该目录用来存放由 ...

  2. Python爬虫-百度模拟登录(一)

    千呼万唤屎出来呀,百度模拟登录终于要呈现在大家眼前了,最近比较忙,晚上又得早点休息,这篇文章写了好几天才完成.这个成功以后,我打算试试百度网盘的其他接口实现.看看能不能把服务器文件上传到网盘,好歹也有 ...

  3. Typora+PicGo+Gitee笔记方案

    前言:需要学习的知识太多,从一开始就在寻找一款能让我完全满意的编辑器,然而一直都没有令我满意的.在前两天Typora新版本更新后,总算是拥有了一套我认为很完美的笔记方案:使用Typora编写markd ...

  4. 彻底消灭if-else嵌套

    一.背景 1.1 反面教材 不知大家有没遇到过像横放着的金字塔一样的if-else嵌套: if (true) { if (true) { if (true) { if (true) { if (tru ...

  5. Asp.Net Core EndPoint 终点路由工作原理解读

    一.背景 在本打算写一篇关于Identityserver4 的文章时候,确发现自己对EndPoint -终结点路由还不是很了解,故暂时先放弃了IdentityServer4 的研究和编写:所以才产生了 ...

  6. 基于Vue的机器学习平台前端

    项目演示地址:http://vidanao.com/ml>注意1:前端兼容性不太好,360浏览器比较兼容; >注意2:此vidanao.com也是我的个人博文主页,但目前还没部署 源码地址 ...

  7. 【jQuery学习日记】jQuery实现滚动动画

    需要实现的效果 样式分析: 2个主要部分,头部的标题和导航部分,和主要的功能实现区域: 1.头部 <div id="header"> <h1>动漫视频< ...

  8. python框架Django实战商城项目之用户模块创建

    创建用户APP 整个项目会存在多个应用,需要存放在一个单独的文件包了,所以新建一个apps目录,管理所有子应用. 在apps包目录下穿件users应用 python ../../manage.py s ...

  9. 研究开源源码之Myrmec

    好久没写博客了,自己也弄不清是懒了还是忙了.毕竟白天需要工作,晚上有时候看看资料,有时候陪家人,有时候约朋友......更加累了,可能由于累了就懒得总结了. 今天有同事问我关于代码检查文件类型的问题. ...

  10. 优化一、js

    1.防抖和节流 2.深拷贝和浅拷贝