首页 > ACM题库 > HDU-杭电 > HDU 1805 Expressions-二叉树-[解题报告] C++
2013
12-23

HDU 1805 Expressions-二叉树-[解题报告] C++

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 a program to evaluate an expression if the expression is written in postfix notation (also known as reverse polish notation). In postfix notation, an operator is written behind its two operands, which may be expressions themselves. For example, x y + z w – * is a postfix notation of the arithmetic expression given above. Note that in this case parentheses are not required.

To evaluate an expression written in postfix notation, an algorithm operating on a stack can be used. A stack is a data structure which supports two operations:

1. push: a number is inserted at the top of the stack.
2. pop: the number from the top of the stack is taken out.
During the evaluation, we process the expression from left to right. If we encounter a number, we push it onto the stack. If we encounter an operator, we pop the first two numbers from the stack, apply the operator on them, and push the result back onto the stack. More specifically, the following pseudocode shows how to handle the case when we encounter an operator O:

a := pop();
b := pop();
push(b O a);
The result of the expression will be left as the only number on the stack.

Now imagine that we use a queue instead of the stack. A queue also has a push and pop operation, but their meaning is different:

1. push: a number is inserted at the end of the queue.
2. pop: the number from the front of the queue is taken out of the queue.
Can you rewrite the given expression such that the result of the algorithm using the queue is the same as the result of the original expression evaluated using the algorithm with the stack?

输入:

The first line of the input contains a number T (T ≤ 200). The following T lines each contain one expression in postfix notation. Arithmetic operators are represented by uppercase letters, numbers are represented by lowercase letters. You may assume that the length of each expression is less than 10000 characters.

输出:

For each given expression, print the expression with the equivalent result when using the algorithm with the queue instead of the stack. To make the solution unique, you are not allowed to assume that the operators are associative or commutative.

样例输入:

2
xyPzwIM
abcABdefgCDEF

样例输出:

wzyxIPM
gfCecbDdAaEBF

FILE 11234 - Expressions 1181
50.55%
471
89.60%

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2175

题目类型: 数据结构, 二叉树


题目大意:

一般情况下,都是中缀操作符, 如x+y。然后题目给出了一种后缀操作符的形式, 变成 x y +。 进行后缀操作可以用栈模拟,使用push,pop, 过程和经典的“括号匹配”差不多。 然后要求我们转换成队列的方式,用队列的push和pop(队列的和栈的区别).


解体思路:

一开始没思路, 后来觉得听说是要建树。 这题也是我写的第一道二叉树题。

题目的最关键部分是进行二叉树建树,  以及层次遍历逆序输出,还有利用栈的“括号匹配”思想。 二叉树的基本结构是,父结点都是操作符,子节点都是数字。 对于给出的序列, 从左到右遍历,遇到代表数字的小写则建立一个无儿子的树,然后把根结点指针入栈, 遇到代表操作符的大写字母,则从栈中弹出两个根结点,然后建立一个以大写字母为根,弹出的两个操作数为左右儿子的树,再把这个新树的根结点指针压入栈。如此循环下去。 最后,在栈顶的那个指针就是最后建成的树的根结点。  然后对这颗树进行层次遍历把字母取出来,最后逆序输出即可。

样例输入:

2
xyPzwIM
abcABdefgCDEF

样例输出:

wzyxIPM
gfCecbDdAaEBF

代码:

1. 数组版

10278049 11234 Expressions Accepted C++ 1.512 2012-07-01 12:59:01
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;

class Node{
public:
    char data;
    int left;
    int right;
};

stack<int>st;
queue<int>qu;
Node arr[10005];
char str[10005];
int result[10005], resultIndex;


// 进行广搜层次遍历
void bfs(int root){
    while(!qu.empty()) qu.pop();
    
    qu.push(root);
    result[resultIndex++]=arr[root].data;
    
    while(!qu.empty()){
        int t = qu.front();
        qu.pop();
        if(arr[t].left != -1){
            result[resultIndex++] = arr[arr[t].left].data;
            qu.push(arr[t].left);
        }
        if(arr[t].right != -1){
            result[resultIndex++] = arr[arr[t].right].data;
            qu.push(arr[t].right);
        }
    }
}

void Solve(){
    while(!st.empty()) st.pop();
    
    for(int i=0; i<strlen(str); ++i){
        if(islower(str[i])){
            st.push(i);
            arr[i].data  = str[i];
            arr[i].left  = -1;
            arr[i].right = -1;
        }
        else{
            int right = st.top();
            st.pop();
            int left  = st.top();
            st.pop();
            arr[i].data = str[i];
            arr[i].left = left;
            arr[i].right = right;
            st.push(i);     
        }
    }  
    // 按层次遍历,把字母存在一个栈上(为了逆序输出),然后输出   
    resultIndex = 0;
    bfs(st.top());   

    // 按广搜结果的逆序输出
    for(int i=resultIndex-1; i>=0; --i)
        printf("%c",result[i]);
    printf("\n");
}

int main(){
    freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",str);
        Solve();
    }
    return 0;
}

2. 指针动态内存分配版

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<stack>
#include<queue>
using namespace std;

class Node{
public:
    char data;
    Node* left;
    Node* right;
};

stack<Node*>st;
queue<Node*>qu;
char str[10005];
int result[10005], resultIndex;

// 进行广搜层次遍历
void bfs(Node* root){
    while(!qu.empty()) qu.pop();
    
    qu.push(root);
    result[resultIndex++]=root->data;
    
    while(!qu.empty()){
        Node* t = qu.front();
        qu.pop();
        if(t->left != NULL){
            result[resultIndex++] = t->left->data;
            qu.push(t->left);
        }
        if(t->right != NULL){
            result[resultIndex++] = t->right->data;
            qu.push(t->right);
        }
    }
}

void Solve(){
    while(!st.empty()) st.pop();
    
    for(int i=0; i<strlen(str); ++i){
        if(islower(str[i])){
            Node *temp = new Node;
            temp->data = str[i];
            temp->left = NULL;
            temp->right = NULL;
            st.push(temp);
        }
        else{
            Node* right = st.top();
            st.pop();
            Node* left  = st.top();
            st.pop();
            Node* parent = new Node;
            parent->data = str[i];
            parent->left = left;
            parent->right = right;
            st.push(parent);     
        }
    }  
    // 按层次遍历,把字母存在一个栈上(为了逆序输出),然后输出   
    resultIndex = 0;
    bfs(st.top());   

    // 按广搜结果的逆序输出
    for(int i=resultIndex-1; i>=0; --i)
        printf("%c",result[i]);
    printf("\n");
}

int main(){
    freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",str);
        Solve();
    }
    return 0;
}


——      生命的意义,在于赋予它意义。 

                   原创 http://blog.csdn.net/shuangde800 , By
  D_Double



解题报告转自:http://blog.csdn.net/shuangde800/article/details/7707123


  1. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  2. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }