好吧,又一个不明白的
最普通的栗子不过
http://www.patest.cn/contests/mooc-ds/03-3
/*
title:
问题分类:
输入格式的处理,getchar
初始化,对于tt的
pop和push的gettop操作位置不同
题目中说最多30个节点,即最高5层,但是本体采用存储结构应该是顺序存储
故,还要保存最下面的叶节点的信息,即最高6层,需要63个节点
*/
#include<stdio.h>
#include<string.h>
int stack[70];
int top=-1;
int gettop(){
return stack[top];
}
int pop(){
return stack[top--];
}
void push(int x){
stack[++top]=x;
}
void empty(){
while(top!=-1){
pop();
}
}
struct t{
int data;
int left;
int right;
}tt[70];
//找节点
int find(int x){
for(int i=0;i<70;i++){
if(tt[i].data==x){
return i;
}
}
}
struct act{
char c;
int num;
}action[70];
//判定最后一个节点用cnt
int cnt;
void post(int x,int count){
if(x!=-1){
post(tt[x].left,count);
post(tt[x].right,count);
if(cnt==count){
printf("%d\n",tt[x].data);
cnt++;
}else{
printf("%d ",tt[x].data);
cnt++;
}
}
}
int main(){
freopen("in.txt","r",stdin);
int zu;
while(scanf("%d",&zu)!=EOF){
cnt=1;
empty();
//一定要初始化
for(int i=0;i<70;i++){
tt[i].data=0;
tt[i].left=-1;
tt[i].right=-1;
}
zu*=2;
char ss[10];
for(int i=1;i<=zu;i++){
getchar();
scanf("%s",ss);
if(ss[1]=='o'){
action[i].c='o';
}else{
action[i].c='u';
int n;
getchar();
scanf("%d",&n);
action[i].num=n;
}
if(i<=zu/2){
tt[i].data=i;
}
}
for(int i=1;i<zu;i++){
int x;
if(action[i].c=='o'){
x=find(gettop());
pop();
}else{
push(action[i].num);
x=find(gettop());
}
if(action[i].c=='o'&&action[i+1].c=='o'){
tt[x].right=-1;
}else if(action[i].c=='o'&&action[i+1].c=='u'){
tt[x].right=action[i+1].num;
}else if(action[i].c=='u'&&action[i+1].c=='u'){
tt[x].left=action[i+1].num;
}else if(action[i].c=='u'&&action[i+1].c=='o'){
tt[x].left=-1;
}
}
post(1,zu/2);
}
return 0;
}
测试数据
5
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Pop
4
Push 1
Push 2
Push 3
Pop
Pop
Pop
Push 4
Pop
3
Push 1
Pop
Push 2
Pop
Push 3
Pop
3
Push 1
Push 2
Push 3
Pop
Pop
Pop
3
Push 1
Push 2
Pop
Pop
Push 3
Pop
15
Push 1
Pop
Push 2
Pop
Push 3
Pop
Push 4
Pop
Push 5
Pop
Push 6
Pop
Push 7
Pop
Push 8
Pop
Push 9
Pop
Push 10
Pop
Push 11
Pop
Push 12
Pop
Push 13
Pop
Push 14
Pop
Push 15
Pop
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
15
Push 1
Push 2
Push 3
Push 4
Push 5
Push 6
Push 7
Push 8
Push 9
Push 10
Push 11
Push 12
Push 13
Push 14
Push 15
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
Pop
1
Push 1
Pop