uva-11234-表达式

后缀表达式,使用队列计算,要求计算的结果一样,输出队列的输入串

表达式转二叉树,层次序遍历,先右孩子,然后字符串反转输出

#include <iostream>
#include <sstream>
#include<algorithm>
#include<memory.h>
#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
#define null NULL
const int N = 10000;

struct Node
{
    Node* p;
    Node* r;
    Node* l;
    char key;

    Node()
    {
        p = null;
        r = null;
        l = null;
        key = ‘0‘;
    }

};

void print(queue<Node*> q, string& str)
{
    Node* node = null;
    while (!q.empty())
    {
        node = q.front();
        q.pop();
        str += node->key;
        if (node->r != null)
            q.push(node->r);
        if (node->l != null)
            q.push(node->l);
    }
}

int main()
{
    freopen("d:\\1.txt", "r", stdin);
    int t;
    cin >> t;
    while (t--)
    {
        Node* root;
        string str;
        cin >> str;
        stack<Node*> s;
        int l = str.length();
        for (int i = 0; i < l; i++)
        {
            char c = str.at(i);
            Node* node = new Node();
            node->key = c;
            if (‘A‘ <= c && c <= ‘Z‘)
            {
                Node* l = s.top();
                s.pop();
                Node* r = s.top();
                s.pop();
                node->l = l;
                node->r = r;
            }
            s.push(node);
        }
        root = s.top();
        s.pop();
        queue<Node*> q;
        q.push(root);
        string& str1 = str;
        str="";
        print(q, str1);
        reverse(str.begin(), str.end());
        cout << str << endl;
    }
    return 0;
}
时间: 2024-08-01 19:05:28

uva-11234-表达式的相关文章

UVA - 11234 - Expressions (栈和队列)

UVA - 11234 Expressions Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem E: Expressions Arithmetic e

uva 11234 - Expressions

Problem E: Expressions Arithmetic expressions are usually written with the operators in between the two operands (which is called infix notation). For example, (x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write

UVa 11234 The Largest Clique

找最长的连接的点的数量.用tarjan缩点,思考可知每一个强连通分量里的点要么都选,要么都不选(走别的路),可以动规解决. 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cstring> 6 using namespace std; 7 const int mxn=12000; 8 int top,stack[

uva 11234(二叉树、线性表)

题解:可以根据输入的字符串画一个二叉树出来,然后层次遍历一下就和输出结果顺序一样,所以根据输入把大写字母当做两个小写字母的根节点,将节点即运算结果当做另一个小写字母放进栈里,和另一个字母组建生成新的树放进栈里,直到最后的的根节点也放进了栈里,开始层次遍历,用数组模拟队列进行遍历,注意因为结果的顺序是从右到左,所以注意遍历的方向问题. #include <cstdio> #include <stack> #include <queue> #include <cstd

UVa 327 计算简单C表达式

题意:给出一个C表达式,计算其值.其中操作数只有26个字母,从a到z依次初始值为1到26:操作符有二位的 + 和二位的 - ,和正常意思一样,然后还有一位的自增 ++ 和一位的自减 - - ,都有前缀和后缀两种,都和正常理解的一样.然后计算表达式值,并给出式子中的各变量最后的值.对了,每个变量在表达式中最多只出现一次. 思路:题目最后也有给出思路,就是将所有一位操作符即自增和自减操作符去掉,再将有前缀操作符的变量自增或自减了,然后计算表达式的值,最后将有后缀操作符的变量自增或自减.这里只要在输入

UVA 10700-Camel trading(栈求表达式的最大最小值)

Camel trading Time Limit: 1 second Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So,

Matrix Chain Multiplication UVA 442 (栈 表达式求值)

说说: 其实这道题是栈这个数据结构最经典的运用,即表达式的求值.与一般情况不同的是,此次要求的运算数是矩阵.在整个解析表达式的过程中,无非遇到三类字符,一个是'(',另一个是')',剩下的就是运算数了.首先,在遇到'('的时候,栈指针自动加一,并将栈顶元素的行数和列数都设置为-1,这样就不会和正常的运算数混淆了.如果遇到的是运算数,首先要判断当前的栈顶元素是否为运算数(当然,还要注意栈为空的特殊情况).若是,则直接将新的运算数与栈顶运算数进行计算,否则将新运算数入栈.还有最后一种情况就是遇到')

uva 12219 公共表达式消除

转自:http://blog.csdn.net/xl2015190026/article/details/51927559 题目大意: 见紫书p354 基本思路: 二叉树递归,解决超时问题就用map,虽然我不会写map,这是一个问题: 这个print之所以还是用一个done来标记是因为怕越界,如果用数组就得开的非常大,而且容易越界: 代码如下: #include<stdio.h> #include<iostream> #include<map> #include<

公共表达式消除 uva 12219

自己写的时候不知道怎么处理编号,看了别人的代码才发现可以预留编号,学习一个 今天才知道map需要处理好顺序才能正确查找 #include<iostream> #include<algorithm> #include<cstdio> #include<algorithm> #include<map> #include<cstring> using namespace std; #define LL long long #define m

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde