UVA11234 Expressions
题目的意思实在是读不懂,又是把栈变成队列什么的。。
只是大体的意思就是把后缀表达式变一下。。
抛开意思,事实上就是依据输入建个树,然后倒序输出。。
拿第一个例子说明;大写代表操作符(+ - × /之类的)小写代表数字。
。
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的更多相关文章
- uva-11234 Expressions
Arithmetic expressions are usually written with the operators in between the two operands (which is ...
- ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)
Description Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest Unive ...
- SQL——行值表达式(Row Value Expressions)
概述 最近接触了一个新概念——行值表达式,也叫做行值构造器.这是一个很强大的SQL功能,通常我们所操作的SQL表达式都只能针对一行中的单一字段进行操作比较,而行值表达式可以针对一行中的多个字段进行操作 ...
- android switch语句报错:case expressions must be constant expressions
今天无意中碰见了 case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...
- 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 ...
- 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 ...
- Spring AOP AspectJ Pointcut Expressions With Examples--转
原文地址:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- Using Recursive Common table expressions to represent Tree structures
http://www.postgresonline.com/journal/archives/131-Using-Recursive-Common-table-expressions-to-repre ...
- 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 ...
随机推荐
- POJ1915 BFS&双向BFS
俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...
- 这是一个无效的原路径/url
当我们在SourceTree上新建一个“从URL克隆”的远程项目时,在确认“URL”无误的前提下依然报红色字体“这是一个无效的原路径/URL”错误,我们只需要快捷键“Command + ,”打开Sou ...
- Multipart/form-data POST文件上传
简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form method="post"action=&qu ...
- Android HTTP下载文件并保存到本地或SD卡
想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...
- 金立 M6 (GN8003) 解锁 BootLoader 进入第三方 recovery 刷机 ROOT
首先下载好工具:http://url.cn/5EILbQn 备用连接 :http://pan.baidu.com/s/1c28j7k0 本篇教程教你如何傻瓜式解锁BootLoader并刷入recove ...
- macOS下登录store或者xcode等应用时提示【this action could not be completed】
sudo mkdir -p /Users/Shared sudo chown root:wheel /Users/Shared sudo chmod -R 1777 /Users/Shared === ...
- sqlserver查询分析器在本地服务器查看其它SqlServer服务器内容
exec sp_addlinkedserver 服务器自命名,'',sqloledb,要查询服务器的IP地址 exec sp_addlinkedsrvlogin 服务器自命名,false,null,账 ...
- nim游戏解法(转)
转自:http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617 取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若 ...
- day34-2 类和对象(重点)
目录 类 定义类和对象 __dict__ 和__class__ 创建对象时的底层运作 定义对象独有的特征 init __slots__(了解) 给对象添加属性时的底层运作 类 分类/类别 上述的代码( ...
- eas启动服务器时非法组件
EAS实例启动报系统中存在非法组件,实例启动失败: 组件检查机制,要求除了 $EAS_HOME eas\server\lib: $EAS_HOME \eas\server\deploy\files ...