03-3
来源:互联网 发布:centos7 网络配置文件 编辑:程序博客网 时间:2024/04/30 00:37
好吧,又一个不明白的
最普通的栗子不过
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
0 0
- 03-3
- 2015-03-03numpy(3)
- 2012-03-09-3
- 2012-03-28-3
- 03_Spring2.3_AOP_annotation
- Chapter 3-03
- 2015-03-08-pandas(3)
- 2015-03-12-bbs_v0.3
- CCF2014-03-3命令行选项
- %03d、%3d、%-3d规则
- phpbb 3 源码解析03 --acm_file.php
- Google Guice 入门教程03 - 依赖注入(3)
- OpenGL 练习03 3D Gasket
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- 03-树3 Tree Traversals Again
- Vert.x 3学习笔记---03
- 03-树3 Tree Traversals Again
- Java7之多线程读写锁
- HTML4标签的默认样式列表
- LeetCode--Spiral Matrix
- 在Xcode里面连接MySQL,同时显示出mysql里的数据
- phonegap安装 环境搭建与配置详解( 完整版 提供下载地址)
- 03-3
- Android最火框架集锦以及资源库
- windows visual studio 基于openGL的粒子系统设计
- Android 判断当前设备是手机还是平板
- Unit Testing with Android Studio
- java int 转化为Long integer转化为Long
- git 历史提交记录log(git GUI)图文详解
- #iOS开发笔记#定时器的实现
- shows user location在iOS8上不能使用地图定位