zoukankan      html  css  js  c++  java
  • hdu 1805Expressions(二叉树构造的后缀表达式)

    Expressions

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 253    Accepted Submission(s): 121


    Problem Description
    Arithmetic expressions are usually written with the operators in between the two operands (which is called infix notation). For example, (x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write a program to evaluate an expression if the expression is written in postfix notation (also known as reverse polish notation). In postfix notation, an operator is written behind its two operands, which may be expressions themselves. For example, x y + z w - * is a postfix notation of the arithmetic expression given above. Note that in this case parentheses are not required.

    To evaluate an expression written in postfix notation, an algorithm operating on a stack can be used. A stack is a data structure which supports two operations:

    1. push: a number is inserted at the top of the stack.
    2. pop: the number from the top of the stack is taken out.
    During the evaluation, we process the expression from left to right. If we encounter a number, we push it onto the stack. If we encounter an operator, we pop the first two numbers from the stack, apply the operator on them, and push the result back onto the stack. More specifically, the following pseudocode shows how to handle the case when we encounter an operator O:

    a := pop();
    b := pop();
    push(b O a);
    The result of the expression will be left as the only number on the stack.

    Now imagine that we use a queue instead of the stack. A queue also has a push and pop operation, but their meaning is different:

    1. push: a number is inserted at the end of the queue.
    2. pop: the number from the front of the queue is taken out of the queue.
    Can you rewrite the given expression such that the result of the algorithm using the queue is the same as the result of the original expression evaluated using the algorithm with the stack?

     
    Input
    The first line of the input contains a number T (T ≤ 200). The following T lines each contain one expression in postfix notation. Arithmetic operators are represented by uppercase letters, numbers are represented by lowercase letters. You may assume that the length of each expression is less than 10000 characters.

     
    Output
    For each given expression, print the expression with the equivalent result when using the algorithm with the queue instead of the stack. To make the solution unique, you are not allowed to assume that the operators are associative or commutative.

     
    Sample Input
    2 xyPzwIM abcABdefgCDEF
     
    Sample Output
    wzyxIPM gfCecbDdAaEBF
     
    Source
     
    代码:
     1 //#define LOCAL
     2 #include<cstdio>
     3 #include<stack>
     4 #include<cstring>
     5 #include<cctype>
     6 #include<queue>
     7 using namespace std;
     8 const int maxn=10005;
     9 char ss[maxn];
    10 char ans[maxn];
    11 stack<int>op;
    12 struct node
    13 {
    14    char da;
    15    int lef; //该节点的左孩子
    16    int rig; //该节点的有孩子
    17 }str[maxn];
    18 void bfs(int pos)
    19 {
    20     queue<int>sac;
    21     sac.push(pos);
    22     int tt=0;
    23     while(!sac.empty())
    24     {
    25         int i=sac.front();
    26        ans[tt++]=str[i].da;
    27         sac.pop();
    28      if(str[i].lef!=str[i].rig)
    29      {
    30          sac.push(str[i].lef);
    31          sac.push(str[i].rig);
    32      }
    33     }
    34     while(pos>=0)
    35         printf("%c",ans[pos--]);
    36 }
    37 int main()
    38 {
    39    #ifdef LOCAL
    40      freopen("test.in","r",stdin);
    41    #endif
    42    int cas,i;
    43    scanf("%d",&cas);
    44    while(cas--)
    45    {
    46         scanf("%s",ss);
    47         for(i=0;ss[i];i++)
    48      {
    49          if(islower(ss[i]))
    50         {
    51             op.push(i);
    52             str[i].da=ss[i];
    53             str[i].lef=str[i].rig=-1; //没有孩子
    54         }
    55         else
    56         {
    57           str[i].da=ss[i];
    58           str[i].rig=op.top();
    59             op.pop();
    60           str[i].lef=op.top();
    61            op.pop();
    62           op.push(i);
    63         }
    64      }
    65      bfs(i-1);
    66      puts("");
    67    }
    68  return 0;
    69 }
  • 相关阅读:
    css命名书写规范小结。
    不断学习,充实自己。
    【openGL】画正弦函数图像
    【openGL】画五角星
    【openGL】画圆
    【openGL】画直线
    【网络资料】Astar算法详解
    【Agorithm】一次一密加密解密算法
    【HTML5】 web上的音频
    【sicily】卡片游戏
  • 原文地址:https://www.cnblogs.com/gongxijun/p/4023611.html
Copyright © 2011-2022 走看看