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;
}
时间: 2024-08-01 19:34:57

UVA11234 Expressions的相关文章

[转]8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

Linq技术四:动态Linq技术 -- Linq.Expressions

前面介绍了Linq的三个方面应用:Linq to SQL, Linq to XML和Linq to Object,这篇介绍一下动态Linq的实现方式及应用场景. 命名空间: System.Linq; System.Linq.Expressions; 应用Linq的时候,我们都知道只需要Lambda表达式就行,但有些场景仅仅只使用Data Model的字段名操作是不够的或者不方便的. 场景1:假设我们需要拼接Where条件进行查询,一种方式可以拼接IQueryable的表达式.但我想像写SQL语句

According to TLD or attribute directive in tag file, attribute items does not accep t any expressions

According to TLD or attribute directive in tag file, attribute items does not accep t any expressions 严重: Servlet.service() for servlet jsp threw exception org.apache.jasper.JasperException: /selectorTagtest.jsp(26,8) According to TLD or attribute di

SSIS Expressions 属性

将属性值使用一个表达式来表示,Expression可以是常量,可以是variable,可以是parameter. 下图是一个Execute Sql Task的Expressions, Expressions :A collection of expressions. the evaluation result of each expression is assigned to a property and replaces the value of the property. Property就

一步一步跟我学习lucene(17)---lucene搜索之expressions表达式处理

有时候我们在做lucene的结果展示的时候可能需要对多个列的内容进行计算,根据多个field对应的值做数值方面的运算. lucene自4.6版本起,提供了用于运算的expression模块: expression分为两部分: org.apache.lucene.expressions:提供了字段绑定和相关的表达式参数传递的功能: org.apache.lucene.expressions.js:提供了表达式定义的功能. Expression类使用示例 Expression是提供document的

终极解法According to TLD or attribute directive in tag file, attribute select does not accept any expressions

3天硬是是把这个问题解决了 有时候突然上个厕所灵感就来了 第一次向用JSTL解析xml 然后我想遍历整个xml文档打印出来 居然不让我输入变量 那让我怎么办啊 在网上各种找答案 说什么<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>变为:<%@ taglib prefix="c" uri=http://java.sun.com/jstl/core_rt  %

Using Regular Expressions in Python

1. 反斜杠的困扰(The Backslash) 有时候需要匹配的文本带有'\',如'\python',因为正则表达式有些特殊字符有特殊意义,所以需要前面加上'\'来消除特殊意义,这里匹配的正则表达式是'\\python',这时候如果要编译这个正则表达式需要re.compile('\\\\python'),因为在传递字符串的时候python本身就需要用'\\'来表示'\',也就造成了反斜杠的泛滥. 使用前缀'r'可以解决这个问题:’r'之后的'\'只是这个字符本身而没有特殊意义,比如r'\n'表

关于According to TLD or attribute directive in tag file, attribute value does not accept any expressions异常

在做项目时遇到了这个异常,网上一查才知道 JSTL core库的版本问题. 当我们在web.xml中使用: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> ..... </web-app> 时 我们在jsp才用&

Lambda Expressions表达式使用

很多时候我们在使用Lambda表达式查询时,比如使用Lambda表达式查询用户数据,有时候会用电话或邮箱去查询用户信息,有时候又会用户名去查询用户信息 var user = db.Set<U_User>().Where(c => c.UserName = "nee32"); var user = db.Set<U_User>().Where(c => c.TelePhone = "13888888888"); 其实查询的结果都一样