UVA 11234 Expressions(数据结构,二叉树,栈,队列)

来源:互联网 发布:朗读软件哪个好 编辑:程序博客网 时间:2024/05/17 07:16

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2175

2007/2008 ACM International Collegiate Programming Contest 
University of Ulm Local Contest

Problem E: Expressions

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 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

2xyPzwIMabcABdefgCDEF

Sample Output

wzyxIPMgfCecbDdAaEBF

题目给出后缀表达式(逆波兰表达式)的栈式表达法,求出它的队列表达法

如样例:xyPzwIM,首先构造出它的二叉树,如下图


自底向上,从右往左遍历即得后缀表达式的队列表达法wzyxIPM

这样求解过程就是建树+遍历的过程

建树很好建,小写字母则入栈,大写字母则将栈顶的两个元素弹出并作为它的左右孩子,然后再压入栈中

遍历看似不太好操作,想一想BFS,它是自顶向下,从左往右遍历的,和我们想要的结果相反,所以我们只要按BFS的方法遍历该树,然后再逆序输出即可

#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<vector>#include<map>#include<set>#define sqr(x) ((x)*(x))#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#define mm 10001using namespace std;struct Node{    int i,l,r;}node[mm];stack<int> st;queue<int> que;char s[mm];string str;int main(){    #ifndef ONLINE_JUDGE        freopen("/home/fcbruce/文档/code/t","r",stdin);    #endif // ONLINE_JUDGE    int tt;    scanf("%d",&tt);    while (tt--)    {        while (!st.empty()) st.pop();        while (!que.empty())    que.pop();        scanf("%s",s);        int l=strlen(s);        for (int i=0;i<l;i++)        {            node[i].i=i;            node[i].l=node[i].r=-1;            if (islower(s[i]))            {                st.push(i);                continue;            }            if (isupper(s[i]))            {                node[i].r=st.top();st.pop();                node[i].l=st.top();st.pop();                st.push(i);            }        }        int k=st.top();st.pop();        que.push(k);        str="";        while (!que.empty())        {            int k=que.front();            que.pop();            str+=s[k];            if (node[k].l!=-1)  que.push(node[k].l);            if (node[k].r!=-1)  que.push(node[k].r);        }        reverse(str.begin(),str.end());        puts(str.c_str());    }    return 0;}


1 0
原创粉丝点击