2013
12-23

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

 11234 - Expressions 1181 50.55% 471 89.60%

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

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() {