# 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 }```

0 条评论

• ### HDUOJ-------2493Timer(数学 2008北京现场赛H题)

Timer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav...

• ### hdu----(1950)Bridging signals(最长递增子序列 (LIS) )

Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/3...

• ### poj1611---The Suspects

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: ...

• ### 知识图谱推理与实践(3) -- jena自定义builtin

在第2篇里，介绍了jena的The general purpose rule engine（通用规则引擎）及其使用，本篇继续探究，如何自定义builtin。

• ### Swift进阶-LLDB调试

工欲善其事，必先利其器。程序猿最好的利器就是开发工具，iOS开发者最基本，最关键的一点就是熟练使用Xcode，而LLDB则是Xcode中至关重要的一环。 作为...

• ### Java 15 的新特性： TextBlocks

JDK 15 is the open-source reference implementation of version 15 of the Java SE ...

• ### Oops错误

在at91rm9200下写了一个spi的驱动，加载后，运行测试程序时，蹦出这么个吓人的东西： Unable to handle kernel paging r...

• ### 统计学中的区间估计

统计学中有两大分支——描述性统计学（description stats）和推断性统计学（inference stats）。 推断性统计学中，很重要的一点就是...

### 活动推荐 