zoukankan      html  css  js  c++  java
  • hdu 3350 #define is unsafe(栈的应用)

    http://acm.hdu.edu.cn/showproblem.php?pid=3350

    Problem Description
    Have you used #define in C/C++ code like the code below?
    #include <stdio.h> #define MAX(a , b) ((a) > (b) ? (a) : (b)) int main() {   printf("%d\n" , MAX(2 + 3 , 4));   return 0; }
    Run the code and get an output: 5, right? You may think it is equal to this code:
    #include <stdio.h> int max(a , b) {  return ((a) > (b) ? (a) : (b));  } int main() {   printf("%d\n" , max(2 + 3 , 4));   return 0; }
    But they aren't.Though they do produce the same anwser , they work in two different ways. The first code, just replace the MAX(2 + 3 , 4) with ((2 + 3) > (4) ? (2 + 3) : 4), which calculates (2 + 3) twice. While the second calculates (2 + 3) first, and send the value (5 , 4) to function max(a , b) , which calculates (2 + 3) only once.
    What about MAX( MAX(1+2,2) , 3 ) ? Remember "replace". First replace: MAX( (1 + 2) > 2 ? (1 + 2) : 2 , 3) Second replace: ( ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) > 3 ? ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) : 3). The code may calculate the same expression many times like ( 1 + 2 ) above. So #define isn't good.In this problem,I'll give you some strings, tell me the result and how many additions(加法) are computed.
     
    Input
    The first line is an integer T(T<=40) indicating case number. The next T lines each has a string(no longer than 1000), with MAX(a,b), digits, '+' only(Yes, there're no other characters). In MAX(a,b), a and b may be a string with MAX(c,d), digits, '+'.See the sample and things will be clearer.
     
    Output
    For each case, output two integers in a line separated by a single space.Integers in output won't exceed 1000000.
     
    Sample Input
    6 MAX(1,0) 1+MAX(1,0) MAX(2+1,3) MAX(4,2+2) MAX(1+1,2)+MAX(2,3) MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))
     
    Sample Output
    1 0 2 1 3 1 4 2 5 2 28 14
     
    思路:用栈容器,模拟过程
    #include<iostream>
    #include<stack>
    using namespace std;
    struct Nod
    {
        int val,cnt;
    };
    
    stack<Nod> Ns;
    stack<char> sig;
    
    void s_clear()
    {
        while(!Ns.empty())
        {
            Ns.pop();
        }
        while(!sig.empty())
        {
            sig.pop();
        }
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        getchar();
        while(t--)
        {
            char str[1010];
            gets(str);
            s_clear();
            Nod temp,temp2;
            temp.cnt=temp.val=0;
            int i;
            for(i=0;str[i];i++)
            {
                if(str[i]=='(')
                {
                    sig.push(str[i]);
                    temp.cnt=temp.val=0;
                }
                else if(str[i]==',')
                {
                    while(!sig.empty()&&sig.top()=='+')
                    {
                        temp2=Ns.top();
                        temp.val+=temp2.val;
                        temp.cnt+=temp2.cnt+1;
                        sig.pop();
                        Ns.pop();
                    }
                    Ns.push(temp);
                    temp.cnt=temp.val=0;
                }
                else if(str[i]==')')
                {
                    while(!sig.empty()&&sig.top()=='+')
                    {
                        temp2=Ns.top();
                        temp.val+=temp2.val;
                        temp.cnt+=temp2.cnt+1;
                        sig.pop();
                        Ns.pop();
                    }
                    temp2=Ns.top();
                    if(temp2.val>temp.val)
                    {
                        temp.val=temp2.val;
                        temp.cnt+=2*temp2.cnt;
                    }
                    else
                    {
                        temp.cnt=2*temp.cnt+temp2.cnt;
                    }
                    sig.pop();
                    Ns.pop();
                }
                else if(str[i]=='+')
                {
                    sig.push(str[i]);
                    Ns.push(temp);
                    temp.cnt=temp.val=0;
                }
                else if(str[i]>='0'&&str[i]<='9')
                {
                    temp.val=10*temp.val+str[i]-'0';
                }
            }
            while(!sig.empty()&&sig.top()=='+')
            {
                temp2=Ns.top();
                temp.val+=temp2.val;
                temp.cnt+=temp2.cnt+1;
                sig.pop();
                Ns.pop();
            }
            printf("%d %d\n",temp.val,temp.cnt);
        }
        return 0;
    }
  • 相关阅读:
    《区块链100问》第51集:区块链资产有匿名性
    《区块链100问》第52集:区块链资产能去中心化记账
    《区块链100问》第53集:区块链资产不可复制
    day 5 模块导入、常用模块os shutil sys commands subprocess hashlib json pickle zipfile traceback random datetime pathlib
    apache中配置php支持模块模式、cgi模式和fastcgi模式的实验
    day3--集合、文件操作、字符编码与转换、函数(递归,lambda,filter,map)、字典排序
    练习题目 :if for while else range、xrange、zip
    解决mySQL占用内存超大问题
    my.cnf重要配置参数说明
    chattr与lsattr命令详解
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2839820.html
Copyright © 2011-2022 走看看