uva 11234 - Expressions

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

2
xyPzwIM
abcABdefgCDEF

Sample Output

wzyxIPM
gfCecbDdAaEBF
#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <stack>
#include <list>
#include <sstream>

using namespace std;

#define ms(arr, val) memset(arr, val, sizeof(arr))
#define N 50000
#define INF 0x3fffffff
#define vint vector<int>
#define setint set<int>
#define mint map<int, int>
#define lint list<int>
#define sch stack<char>
#define qch queue<char>
#define sint stack<int>
#define qint queue<int>

struct btree
{
    int l;
    int r;
}bt[N];

char post[N];
sint si;
qint qi;
sch sc;

void build(int i)//建立二叉树(后序遍历左右中)bt和post下标一致
{
    while (post[i])
    {
        if (islower(post[i]))
        {
            bt[i].l = bt[i].r = 0;
        }
        else
        {
            bt[i].r = si.top();
            si.pop();
            bt[i].l = si.top();
            si.pop();
        }
        si.push(i++);
    }
}
void bfs()
{
    int t = si.top();
    qi.push(t);//根节点下标压栈
    sc.push(post[t]);
    si.pop();
    while (!qi.empty())
    {
        t = qi.front();
        qi.pop();
        if (bt[t].l)
        {
            qi.push(bt[t].l);
            sc.push(post[bt[t].l]);
        }
        if (bt[t].r)
        {
            qi.push(bt[t].r);
            sc.push(post[bt[t].r]);
        }
    }
    while (!sc.empty())
    {
        cout<<sc.top();
        sc.pop();
    }
    cout<<endl;
}
int main()
{
    int n;
    cin>>n;
    getchar();
    while (n--)
    {
        gets(post + 1);
        build(1);
        bfs();
    }
    return 0;
}

uva 11234 - Expressions

时间: 2024-11-05 19:40:25

uva 11234 - Expressions的相关文章

UVA - 11234 - Expressions (栈和队列)

UVA - 11234 Expressions Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem E: Expressions Arithmetic e

UVa 11234 The Largest Clique

找最长的连接的点的数量.用tarjan缩点,思考可知每一个强连通分量里的点要么都选,要么都不选(走别的路),可以动规解决. 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cstring> 6 using namespace std; 7 const int mxn=12000; 8 int top,stack[

UVa 10157 - Expressions

题目:给你n个括号,求合法的匹配中,深度不超过d的组合数. 分析:组合,计数,dp,大整数. 这个题目很像卡塔兰数,不过深度有限制,可以利用卡塔兰数的递推公式求解: 设C(k,d)为k对括号形成深度不超过d的合法匹配方法数:则有: C(k,d)= Σ(C(i,d-1)*C(k-1-i,d)) { i 取0到 k-1 } (按卡塔兰数递推,k对括号分成两组,左边i个生成串A,右边k-1-i个生成串B, 还有一对在A外,形成(A)B形式:这时A的深度最大为d-1,B的深度最大为d) 因此,深度为d的

uva 11234(二叉树、线性表)

题解:可以根据输入的字符串画一个二叉树出来,然后层次遍历一下就和输出结果顺序一样,所以根据输入把大写字母当做两个小写字母的根节点,将节点即运算结果当做另一个小写字母放进栈里,和另一个字母组建生成新的树放进栈里,直到最后的的根节点也放进了栈里,开始层次遍历,用数组模拟队列进行遍历,注意因为结果的顺序是从右到左,所以注意遍历的方向问题. #include <cstdio> #include <stack> #include <queue> #include <cstd

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

uva 327 - Evaluating Simple C Expressions

 Evaluating Simple C Expressions  The task in this problem is to evaluate a sequence of simple C expressions, buy you need not know C to solve the problem! Each of the expressions will appear on a line by itself and will contain no more than 110 char

Evaluating Simple C Expressions UVA 327

说说: 这道题的题意就是求例如:a+b--+c++ 的值,其中a=1,b=2...依次类推.最后要求输出整个表达式的值和每个在表达式中出现过的变量最后的值.思路其实比较简单,先将整个表达式读取,然后遍历,遇到遍历时停下,并判断其是否有前缀++或--,并且找到该变量的运算符(如果有的话),接下来判断是否有后缀--或++,当然其间的空格是要忽略的.至于那些遍历是用一个大小为26的数组保存,没有出现过则值为0,否则前缀或后缀--则值为1,++则为2.最后遍历一下数组,把结果输出即可.总感觉这次代码写得

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest