uva11234

题目:
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?
Input
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.
Output
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.
Sample Input
2
xyPzwIM
abcABdefgCDEF
Sample Output
wzyxIPM
gfCecbDdAaEBF

思路:

给出后缀表达式,将后缀表达式(相当于后序遍历)转化为层序遍历的逆序。利用后序遍历模拟建立树。可是不知道为什么我的代码runtime error了。。。差不多的代码我用栈就runtime error了。

代码:

#include <iostream>
#include<stack>
#include <cstring>
using namespace std;
char str[10010];
int rec[10010];
stack<char> sta;
struct Node
{
    char data;
    int left, right;
};
Node N[10010];
int main()
{
    int n;
    int i;
    int num = 0;
    int root;
    cin >> n;
    while (n--)
    {
        while (!sta.empty())
            sta.pop();
        cin >> str;
        root = strlen(str)-1 ;
        for (i = 0; i < strlen(str); i++)
        {
            N[i].data = str[i];
            N[i].left = -1;
            N[i].right = -1;
            if (isupper(str[i]))
            {
                if (sta.size() > 1)
                {
                    N[i].right = sta.top();
                    sta.pop();
                    N[i].left = sta.top();
                    sta.pop();
                }
            }
            sta.push(i);
        }
        int front = 0, rear = 1;
        num = 0;
        rec[0] = root;
        while (front < rear)
        {
            int t = rec[front++];
            if (N[t].left != -1) rec[rear++] = N[t].left;
            if (N[t].right != -1) rec[rear++] = N[t].right;
            num++;
        }
//      N[rec[0]].data = '\0';
        for (i = num -1; i >=0; i--)
            printf("%c", N[rec[i]].data);
        printf("\0");
        printf("\n");
    }
    return 0;
}

别人的代码:

#include<stdio.h>
#include<string.h>
#define MAXN 10000

struct Node
{
 char data;
 int left,right;      //存的是anode数组的下标 
};
void bfs(int root);
int build(char *str);

Node anode[MAXN];
int stack[MAXN+1];
char exp[MAXN+1];

int main()
{
 int n;
 scanf("%d",&n);
 while(n-->0)
 {
  scanf("%s",exp);
  int root=build(exp);//printf("%d\n",root);   
  bfs(root);     
 }
 return 0;   
}

void bfs(int root)
{
 int que[MAXN];
 char ans[MAXN];
 int cnt=0;
 int front=0,rear=1;
 que[0]=root;
 while(front<rear)
 {
  int t=que[front++];
  ans[cnt++]=anode[t].data;
  if(anode[t].left!=0) que[rear++]=anode[t].left;
  if(anode[t].right!=0) que[rear++]=anode[t].right;                
 }    
 ans[cnt]='\0';
 //print      //printf("%s\n",ans);
 for(int i=0;i<cnt;++i)
  printf("%c",ans[cnt-i-1]);
 printf("\n");
}

int build(char *str)
{
 int cnt=1;//anode数组的下标0元素表示不存在该结点 
 int top=0;//始终指向栈顶元素,stack[0]不用 
 while((*str)!='\0')
 {
  anode[cnt].data=*str;
  if(*str>='a'&&*str<='z')
  {
   anode[cnt].left=anode[cnt].right=0;
   stack[++top]=cnt;
  }                  
  else
  {
   if(top>1) 
   {
    anode[cnt].right=stack[top--];
    anode[cnt].left=stack[top--];
   }  
   stack[++top]=cnt;
  }
  str++;
  cnt++;
 }//while
 return cnt-1;    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值