Catch Overflow!

https://codeforces.com/contest/1175/problem/B

题解:栈+模拟+标记

1、用栈记录for嵌套;

2、变量now记录当前栈内乘积;

3、当now大于的不再向栈内添加;

4、出栈时判断当前for是否在栈内;

5、ans大于时打标记;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const ll M=(1ll<<32)-1;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
ll ans,cnt,flag,temp,sum;
int vis[N];
char str[10];
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    stack<int>st;
    temp=1;
    while(n--){
        cin>>str;

        if(str[0]=='f'){
            scanf("%d",&m);
            if(flag){
                continue;
            }
            st.push(m);
            if(temp<=M)
                vis[st.size()]=1,temp*=m;
        }else if(str[0]=='e'){
            if(flag){
                continue;
            }
            m=st.top();
            if(vis[st.size()])
                vis[st.size()]=0,temp/=m;
            st.pop();
        }else{
            ans+=temp;
            if(ans>M)flag=1;
            if(flag){
                continue;
            }
        }
    }
    if(!flag)cout<<ans<<endl;
    else cout<<"OVERFLOW!!!"<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务