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

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

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

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

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. C - Domino piling

    Problem description You are given a rectangular board of M × N squares. Also you are given an unlimi ...

  2. Spring的AOP机制---- AOP的注解配置---- AOP的注解配置

    3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...

  3. Eclipse 每次ctrl-c ctrl-v 就变慢?

    继续闲着,所以继续写 大小: 60.7 KB 查看图片附件

  4. 团队作业-Beta版本发布

    这个作业属于哪个课程  <课程的链接>            这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 Beta版本发布报 ...

  5. servlet_获取初始化参数

    在web.xml的<servlet>标签中增添 <init-param> <param-name>XXX</param-name> <param- ...

  6. 【转载】浏览器缓存详解:expires cache-control last-modified

    https://www.cnblogs.com/caiyanhu/p/6931624.html 下面的内容展示了一个常见的 Response Headers,这些 Headers 要求客户端最多缓存 ...

  7. mac nwjs入门

    NW.js由node-webkit项目发展而来其实很多东西官网上都有.但是鉴于搜索引擎(百度,google)搜索到的相关文章,让人看的很不明白.所以决定写下此篇文章. 官网:https://nwjs. ...

  8. PHP并发IO编程实践

    PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...

  9. C# 发起Get和Post请求

    public class ApiHelper { //contentType application/json or application/xml public string HttpGet(str ...

  10. unittest的case和报告生成方法

    #coding=utf-8from appium import webdriverimport unittestimport HTMLTestRunnerclass CaseTest(unittest ...