zoukankan      html  css  js  c++  java
  • CodeForces

    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 nntimes;
    • 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 xx becomes 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.

    Input

    The first line contains a single integer ll (1l1051≤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 (1n1001≤n≤100) — for loop;
    • end — every command between "for nn" and corresponding "end" is executed nntimes;
    • 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
    9
    add
    for 43
    end
    for 10
    for 15
    add
    end
    add
    end
    
    Output
    161
    
    Input
    2
    for 62
    end
    
    Output
    0
    
    Input
    11
    for 100
    for 100
    for 100
    for 100
    for 100
    add
    end
    end
    end
    end
    end
    
    Output
    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 2321232−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!!!
    ");
        else printf("%I64d
    ",y);
        return 0;
    }
  • 相关阅读:
    linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)
    windows编程之GDI基础--获取设备内容属性(三)
    windows编程之GDI基础--设备内容(二)
    windows编程之GDI基础(一)
    windows编程之滚动条(新式滚动条函数)
    window编程之滚动条(老式滚动条)
    来博客园混了...
    实现一个4位加减法与或运算选择器(作业)
    黑客初级知识(四)
    黑客初级知识(三)
  • 原文地址:https://www.cnblogs.com/yzm10/p/11109944.html
Copyright © 2011-2022 走看看