UVA 11234 (두 갈래 트리 앞 순서 후속 라우팅 방법)
Expressions
Font Size:
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:
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:
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 followingT 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 than10000 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
정상적인 표현식: 트리의 중차 반복
역 폴란드식: 나무의 뒷차례 두루 훑어보기
폴란드식: 나무의 앞길이 두루 다니다
이 세 가지 특성에 따라 나무를 세우면 차원이 두루 돌아다니며 결과를 얻을 수 있다
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");
using namespace std;
#define maxn 45000
#define INF 0x3f3f3f3f
int u[maxn],v[maxn],fir[maxn],nex[maxn];
char s[maxn];
int st[maxn];
int top;
int e_max;
void init()
{
memset(fir,-1,sizeof fir);
e_max=0;
}
void add_edge(int s,int t)
{
int e=e_max++;
u[e]=s;
v[e]=t;
nex[e]=fir[s];
fir[s]=e;
}
int que[maxn];
void bfs(int root)
{
int l=0,r=-1;
que[++r]=root;
while(l<=r)
{
int p=que[l++];
for(int i=fir[p];~i;i=nex[i])
{
que[++r]=v[i];
}
}
for(int i=l-1;i>=0;i--)
{
printf("%c",s[que[i]]);
}
printf("
");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
top=-1;
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++)
{
if(s[i]>='a'&&s[i]<='z')
{
st[++top]=i;
}
else
{
int a=st[top--];
int b=st[top--];
add_edge(i,a);
add_edge(i,b);
st[++top]=i;
}
}
int root=st[top--];
bfs(root);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.