uva11234(树)
来源:互联网 发布:大承网络 空中网 编辑:程序博客网 时间:2024/05/23 00:13
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:
- 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 10000characters.
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
//一棵树的后序,然后逆序输出
#include<stdio.h> #include<string.h>int max,u;char list[8000][2000],str[10005];int rear[8000];//每层的个数void dfs(int v)//v是层数,当v是0时在根节点{if(str[u]>='A'&&str[u]<='Z') list[v][rear[v]++]=str[u];if(v>max) max=v;if('a'<=str[u]&&str[u]<='z') {list[v][rear[v]++]=str[u];return;//小写字母是终端节点}u--;dfs(v+1);u--;dfs(v+1);}main(){int i,j,L,T;//freopen("d:\\pp.txt","r",stdin);scanf("%d",&T);getchar();while(T--){memset(list,' ',sizeof(list));memset(rear,0,sizeof(rear));gets(str);L=strlen(str);max=0;u=L-1;dfs(0);for(i=max;i>=0;i--){for(j=0;j<rear[i];j++)printf("%c",list[i][j]);}printf("\n");}return 0;}
- uva11234(树)
- uva11234
- uva11234-
- uva11234
- uva11234 Expression (二叉树重建+层次遍历)
- 树+栈+队列(表达式树)uva11234
- UVA11234 - Expressions
- Uva11234 - Expressions
- UVA11234- Expressions
- UVA11234 Expressions
- UVa11234 表达式
- UVA11234 Expressions【BFS】
- Trie树(字典树、字母树)
- Trie树(前缀树,字典树)
- trie树(字典树)&& 后缀树
- Trie树(字典树、前缀树)
- 字典树(trie树)
- Trie树(字典树)
- hdu 2546 饭卡【贪心+01背包基础题】
- 解决httpd: Could not reliably determine the server's fully qualified domain name
- Android ApiDemos示例解析(7) Activity之间跳转 --又又哥
- 线程同步
- ios开发之--判断当前设备的类型
- uva11234(树)
- Oracle-VPD简析
- 程序研究-
- json添加属性
- android基础_imageview的scaleType,listview的cacheColorHint和android:listSelector
- struts2-core-2.0.14更新到2.3.15
- 随笔——学习的一些步骤及注意点(不断更新)
- hdu 1021 数学题
- HTTP 304 的理解