uva 548
来源:互联网 发布:网络迷情女人视频 编辑:程序博客网 时间:2024/06/13 02:33
根据中序遍历和后序遍历构建树
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <sstream> using namespace std;const int maxv = 10000+10;int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];int n;bool read_list(int* a){ string line; if(!getline(cin,line)) return false; stringstream ss(line); n = 0; int x; while(ss>>x) a[n++] = x; return n>0;}int build(int r1,int l1,int r2,int l2){ if(r1>l1) return 0; int root = post_order[l2]; int p = r1; while(in_order[p]!=root) p++; int cnt = p-r1; lch[root] = build(r1,p-1,r2,r2+cnt-1); rch[root] = build(p+1,l1,r2+cnt,l2-1); return root;}int best , best_sum;void dfs(int u,int sum){ sum += u; if(!lch[u]&&!rch[u]){ if(sum<best_sum ||(sum==best_sum&&u<best)){ best = u; best_sum = sum; } } if(lch[u]) dfs(lch[u],sum); if(rch[u]) dfs(rch[u],sum); }int main(){ while(read_list(in_order)){ //memset(lch,0,sizeof(lch)); //memset(rch,0,sizeof(rch)); read_list(post_order); build(0,n-1,0,n-1); best_sum = 1000000000; dfs(post_order[n-1],0); cout<<best<<endl; } return 0; }
0 0
- uva 548
- UVA 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- UVa 548
- uva 548
- UVA 548
- Uva 548
- UVA 548
- UVa 548 Tree
- uva 548 Tree
- uva 548 - Tree
- Java IO 流读取文件
- STL之智能指针
- C++之STL中sort函数的内部实现(一)
- Android自动测试之monkey测试
- 使用STM32CubeMX开发三:按键中断实验
- uva 548
- [ios] xcode实用插件(持续更新)
- pycuda的安装和错误解决(含boost安装编译的详细过程)
- Using UrbanCode Deploy for Continuous Delivery of Dockerized Applications
- 队列在Java类库中的链式表示及实现——LinkedList
- [python] 类常用的内置方法
- c# is和as的区别
- 如何在Ubuntu QML应用中判断应用的方位(landscape或portrait)
- 预览doc、xslx等Office文档和pdf以及Quicklook中QLPreviewController