POJ 3367 Expressions(資料結構-二叉樹)

來源:互聯網
上載者:User

標籤:des   style   http   color   os   strong   io   資料   

Expressions
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2314   Accepted: 760

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

2xyPzwIMabcABdefgCDEF

Sample Output

wzyxIPMgfCecbDdAaEBF

Source

Ulm Local 2007


題目大意:

給你一個滿二叉樹的後序遍曆,小寫字母表示葉子節點,將這課樹按照從下到上,從右至左順序輸出。


解題思路:

先建立二叉樹,用堆棧實現。

然後輸出,用隊列實現。


解題代碼:

#include <iostream>#include <string>#include <cstdio>#include <stack>#include <queue>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int maxn=11000;struct node{    int l,r;    char c;}e[maxn];int cnt;char st[maxn];void initial(){    int len=strlen(st);    for(int i=0;i<=len;i++){        e[i].l=e[i].r=-1;    }    cnt=0;}void solve(){    int len=strlen(st);    stack <int> v;    for(int i=0;i<len;i++){        if(st[i]>='a' && st[i]<='z'){            e[cnt].c=st[i];            v.push(cnt);            cnt++;        }else{            int r=v.top();            v.pop();            int l=v.top();            v.pop();            e[cnt].l=l;            e[cnt].r=r;            e[cnt].c=st[i];            v.push(cnt);            cnt++;        }    }}void output(){    string ans;    queue <int> q;    q.push(cnt-1);    while(!q.empty()){        int s=q.front();        q.pop();        ans.push_back(e[s].c);        if(e[s].l!=-1) q.push(e[s].l);        if(e[s].r!=-1) q.push(e[s].r);    }    reverse(ans.begin(),ans.end());    printf("%s\n",ans.c_str());}int main(){    int t;    scanf("%d",&t);    while(t-- >0){        scanf("%s",st);        initial();        solve();        output();    }    return 0;}



               

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.