程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> uva-11234 Expressions

uva-11234 Expressions

編輯:C++入門知識

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:

push: a number is inserted at the top of the stack.
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:

push: a number is inserted at the end of the queue.
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 Specification
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 Specification
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題目意思:求一個字符序列,該序列使得當它使用隊列且使用與棧相同的操作序列,表達示結果一樣。解題思路:當你建立表達示樹的時候,不難發現一個規律,所求的字符序列是表達示樹的層序遍歷產生的序列的逆序。所以我們先建樹,然後層序遍歷就行了。

#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cctype>
#include<queue>
using namespace std;

struct node
{
    char data;
    node*left;
    node*right;
    node(int d):data(d),left(0),right(0){};
};

node* build(string s)
{
    stack<node*>st;
    for(unsigned i=0;i<s.size();i++)
    {
        if(isupper(s[i]))
        {
            node*right=st.top();st.pop();
            node*left=st.top();st.pop();
            node*root=new node(s[i]);
            root->right=right;
            root->left=left;
            st.push(root);
        }
        else
        {
            st.push(new node(s[i]));
        }
    }
    return st.top();
}

string get_ans(node* root)
{
    queue<node*>q;
    q.push(root);
    string ans;
    while(!q.empty())
    {
        node*cur=q.front();q.pop();
        ans+=cur->data;
        if(cur->left)q.push(cur->left);
        if(cur->right)q.push(cur->right);
    }
    reverse(ans.begin(),ans.end());
    return ans;
}

int main()
{
    int n;
    string s;
    cin>>n;
    while(n--)
    {
        string ans;
        cin>>s;
        node* root=build(s);
        cout<<get_ans(root)<<endl;
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved