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 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 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 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.

思路:

模拟,我用递归写的。

用stack也可以

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const unsigned int MAXV = (1LL<<32)-1;
const int MAXN = 1e5+10;
int n;
bool flag = true;

struct Op
{
    string op;
    LL va;
}oper[MAXN];
int step = 1;

LL Dfs(LL lops)
{
    if (step > n)
        return 0;
    LL res = 0;
    while (oper[step].op[0] == 'a')
    {
        step++;
        res++;
    }
//    cout << "res1:" << res << endl;
    while (oper[step].op[0] == 'f')
    {
        res += Dfs(oper[step++].va);
        if (res > MAXV)
            flag = false;
        while (oper[step].op[0] == 'a')
        {
            step++;
            res++;
        }
    }
//    cout << "res2:" << res << endl;
    if (1LL*lops*res > MAXV)
        flag = false;
//    cout << "res3:" << lops*res << ' ' << step << endl;
    step++;
    return lops*res;
}

int main()
{
//    freopen("test.in", "r", stdin);
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        cin >> oper[i].op;
        if (oper[i].op[0] == 'f')
            cin >> oper[i].va;
    }
    LL res = Dfs(1);
    if (!flag)
        cout << "OVERFLOW!!!" << endl;
    else
        cout << res << endl;

    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10984224.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值