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 ...
随机推荐
- SQL Server 行转列,列转行
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...
- git上
## 建立本地版本库 ## 本地版本库与远程关联 ## 修改文件并提交 ## 创建分支,修改文件合并至master 1. git的由来 linux系统是很多开发者贡献代码不断完善的,linux的创始人 ...
- DOM 介绍
什么时DOM DOM:文档对象模型.DOM为文档提供了结构化表示,并定义了如何通过脚本来范文文档结构.目的起始就是为了能让js操作html元素而指定的一个规范. DOM就是由节点组成的. 解析过程 H ...
- HBase、Hive、MapReduce、Hadoop、Spark 开发环境搭建后的一些步骤(export导出jar包方式 或 Ant 方式)
步骤一 若是,不会HBase开发环境搭建的博文们,见我下面的这篇博客. HBase 开发环境搭建(Eclipse\MyEclipse + Maven) 步骤一里的,需要补充的.如下: 在项目名,右键, ...
- CSS元素水平垂直居中的方法
1. 元素水平居中 1.1 设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...
- vs2017 创建项目推送到Git上
地址 在从本地往云上推送的时候遇到了这样的问题 将分支推送到远程存储库时遇到错误: rejected Updates were rejected because the remote contains ...
- c# ado.net eftity framework 返回多表查询结果
public static IQueryable GetWeiXinTuWenList() { using (var Model = new Model.WeiXinEntities()) { var ...
- Java中final,finally和finalize区别
Day11_SHJavaTraing_4-18-2017 Java中final,finally和finalize区别 1.final—修饰符(关键字) ①final修饰类,表示该类不可被继承 ②fin ...
- Cell期刊论文:为什么计算机人脸识别注定超越人类?(祖母论与还原论之争)
终于找到ML日报的微信链接,抄之...................................... 请拜访原文链接:[祖母论与还原论之争]为什么计算机人脸识别注定超越人类?评价: ...
- c# 读取 XML
XmlDocument xmldoc = new XmlDocument(); string xmlPath = HttpContext.Server.MapPath("~/*****.xm ...