题目的意思实在是读不懂,又是把栈变成队列什么的。。

只是大体的意思就是把后缀表达式变一下。。

抛开意思,事实上就是依据输入建个树,然后倒序输出。。

拿第一个例子说明;大写代表操作符(+ - × /之类的)小写代表数字。

xyPzwIM:就是指 (xPy) M (zIw) 就像是两数相乘1 × 2写成 12×;

变成树的样子就是

     M

    / |

   P   I

  / |  / |

 x  y z  w

然后把这棵数倒着输出 wzyxIPM。

建树时就是碰到小写,就建个小树。左子树右子数都是空,压入栈。

碰到大写,也要建个小树,并把栈顶两个元素取出来,作为做子树和右子树。。在把新树压入栈

建完后栈顶就是这个树的根,採用广搜遍历即可。

AC代码:

#include<iostream>
#include<string>
#include<stack>
#include<stdlib.h>
#include<queue>
using namespace std; const int N = 100010;
struct node{
char value;
node *left,*right;
};
int main () {
int T;
stack<node*> sta;
queue<node*> que;
cin >> T;
while (T--) {
string str;
cin >> str;
for (int i = 0 ; i < str.size() ;i++) {
if (str[i] >='a' && str[i] <='z') {
node* u =(node*)malloc(sizeof(node));
u -> value = str[i];
u -> left = u -> right = NULL;
sta.push(u);
}
if (str[i] >= 'A' && str[i] <= 'Z') {
node* u = (node*)malloc(sizeof(node));
u -> value = str[i];
u -> right = sta.top();
sta.pop();
u -> left = sta.top();
sta.pop();
sta.push(u);
}
}
node* u = sta.top();
que.push(u);
int n = 0;
char res[N];
while (!que.empty()) {
u = que.front();
if(u -> left != NULL)
que.push(u -> left);
if(u -> right != NULL)
que.push(u -> right);
res[n++] = u -> value;
que.pop();
}
for (int i = n - 1 ;i >= 0 ;i--)
cout << res[i] ;
cout << endl;
while (!sta.empty())
sta.pop();
}
return 0;
}

UVA11234 Expressions的更多相关文章

  1. uva-11234 Expressions

    Arithmetic expressions are usually written with the operators in between the two operands (which is ...

  2. ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)

    Description   Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest Unive ...

  3. SQL——行值表达式(Row Value Expressions)

    概述 最近接触了一个新概念——行值表达式,也叫做行值构造器.这是一个很强大的SQL功能,通常我们所操作的SQL表达式都只能针对一行中的单一字段进行操作比较,而行值表达式可以针对一行中的多个字段进行操作 ...

  4. android switch语句报错:case expressions must be constant expressions

    今天无意中碰见了   case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...

  5. RDLC An unexpected error occurred while compiling expressions. Native compiler return value: '-1073741511'

    One of my web project, which has a rdlc file using some expressions, was working fine while developi ...

  6. According to TLD or attribute directive in tag file, attribute test does not accept any expressions

    HTTP Status 500 - /WEB-INF/views/emp/list.jsp (line: 30, column: 4) According to TLD or attribute di ...

  7. Spring AOP AspectJ Pointcut Expressions With Examples--转

    原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...

  8. Using Recursive Common table expressions to represent Tree structures

    http://www.postgresonline.com/journal/archives/131-Using-Recursive-Common-table-expressions-to-repre ...

  9. According to TLD or attribute directive in tag file, attribute end does not accept any expressions

    问题描述: 在 JSP 页面中使用 JSTL 标签库,访问 JSP 页面时抛出了如下异常信息: org.apache.jasper.JasperException: /WEB-INF/manageUs ...

随机推荐

  1. 辨析 singleton 和 prototype

    <bean id="person1" class="com.bean.life.Person"> <property name="n ...

  2. Android 权限管理(持续整理)

    1. Android 6.0之后,APP可以直接安装,运行时再询问用户授予相关权限,此时系统弹出一个对话框,(这个对话框不能由开发者定制) 同时用户也可以在手机的“设置”中对于某个App进行权限管理 ...

  3. php正则表达式应用

    正则表达式 1.替换“/\d/”,“#”,$str:正则表达式\d 数字,替换为#,字符串 $str = "2hello 5li 6lei"; echo preg_replace( ...

  4. (转载) ORA-12537:TNS连接已关闭

    今天在远程客户端配置EBS数据库连接的时候发生“ORA-12537:TNS连接已关闭”的错误.进入服务器运行如下命令:$tnsping VIS 这里VIS如果定义服务名,可以写成 $ tnsping ...

  5. printf 打印较长字符

  6. marquee标签弹幕效果

    播放个视频的时候看到很有趣的弹幕,想着前端能不能做个弹幕效果.弹幕是滚动的,所以首先想到了<marquee>标签.但事实上,<marquee>标签不是w3c的标准,只是主流的浏 ...

  7. Android LinearLayout整个布局设置不可点击

    1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow ) <?xml version="1.0" encoding=" ...

  8. 继承(day09)

    二十一 继承(Inheritance) ... 子类的构造函数和析构函数 5.1 子类的构造函数 )如果子类构造函数没有显式指明基类子对象的初始化方式,那么该子对象将以无参方式被初始化. )如果希望基 ...

  9. Java常量池详细说明

    java常量池技术  java中的常量池技术,是为了方便快捷地创建某些对象而出现的,当需要一个对象时,就可以从池中取一个出来(如果池中没有则创建一个),则在需要重复创建相等变量时节省了很多时间.常量池 ...

  10. [jzoj5791]【NOIP2008模拟】阶乘 (数学)

    传送门 Description 有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值. Input 共两行. 第一行一个正整数n. 第二行n个正 ...