这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试。

这道题有两种解决方案:

即先建树,再遍历和边建树边遍历。这两种方案经过实践证实效率相差不太多。应该主要耗时的是cin stringstream 之类的输入函数。

另外,通过这道题领悟了一个非常重要的事情:

一定要清空上组数据使用过的数组,否则后果很严重!!!!!!

除非你确定数组清不清无所谓。

在这里也可以稍微用一些技巧。我在输入的同时将所需要的数组对应下标设为初始值,这样能节省很多时间,特别是在数组非常大的时候,这样通过这种方法能节约近一半的时间:

memset(s,0,sizeof(s));

while(t>=0){
    a[n++] = x;
    lch[x]=0;
    rch[x]=0;
}

这里的输入过程,我采用了两种方案

bool read_line1(int* a){
    if(!fgets(s,sizeof(s),stdin))return false;
    n=0;
    int t=0;
    int x;
    while(t>=0){
        sscanf(&s[t], "%d", &x);
        t = strchr(&s[t], ' ') + 1 - &s[0];
        a[n++] = x;
        lch[x]=0;
        rch[x]=0;
    }
    return n>0;
}

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;
}

以下是完成AC代码1

#include <sstream>
#include <string>
#include <cstdio>
#include <set>
#include <cstring>
using namespace std;
const int maxn = 100000 + 10;
int n;
int in_order[maxn],post_order[maxn],lch[maxn], rch[maxn];
char s[maxn];
bool read_line(int* a){
   
}
bool read_line1(int* a){
    if(!fgets(s,sizeof(s),stdin))return false;
    n=0;
    int t=0;
    int x;
    while(t>=0){
        sscanf(&s[t], "%d", &x);
        t = strchr(&s[t], ' ') + 1 - &s[0];
        a[n++] = x;
        lch[x]=0;
        rch[x]=0;
    }
    return n>0;
}
int best,best_sum;
set<int> dict;
int build(int L1, int R1, int L2, int R2, int sum){
    if(L1 > R1){
        return 0;
    }
    int root = post_order[R2];
    sum += root;

if(L1==R1 && !lch[root] && !rch[root]){
                   // printf("%d...%d\n",root,sum);

if(sum<best_sum || sum == best_sum && root<best){
            best_sum = sum;
            best = root;
        }
    }

int p = L1;
    while(in_order[p] != root) p++;
    int cnt = p- L1;
    lch[root] = build(L1,p-1,L2,L2+cnt-1,sum);
    rch[root] = build(p+1,R1,L2+cnt,R2-1,sum);
    return root;
}
void dfs(int u,int sum){
    sum += u;
    if(!lch[u] && !rch[u]){
        if(sum<best_sum || sum == best_sum && u<best){
            best_sum = sum;
            best = u;
        }
    }
    if(lch[u])dfs(lch[u], sum);
    if(rch[u])dfs(rch[u], sum);
}
int main(){
    #ifdef DEBUGI
    freopen("6.8.in","r",stdin);
   // freopen("6.8.out","w",stdout);
    #endif
    #ifdef DEBUGO
    //freopen("6.8.in","r",stdin);
    freopen("6.8.out","w",stdout);
    #endif
    //read_line(in_order);
   
    while(read_line(in_order)){
        read_line(post_order);
        int r=post_order[n-1];
        best_sum = 1000000000;
        build(0,n-1,0,n-1,0);
        //dfs(post_order[n-1],0);
        printf("%d\n",best);
    }
   
    return 0;
}

AC代码2(基本与课本解法一致)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
const int maxv = 10000 + 10;
int best_sum ,best;
int in_order[maxv],post_order[maxv],lch[maxv],rch[maxv];
int n;

int build(int  L1, int R1 ,int L2, int R2){
    if(L1>R1) return 0;
    int root = post_order[R2];
    int p = L1;
    while(in_order[p] != root) p++;
    int cnt=p-L1;
    lch[root]=build(L1,p-1,L2,L2+cnt-1);
    rch[root]=build(p+1,R1,L2+cnt,R2-1);
    return root;
}
void dfs(int u, int sum){
    sum+=u;
    if(!lch[u] && !rch[u]){
   //     printf("*%d*%d*\n",u,sum);
        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);
}
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 main(){
    #ifdef DEBUGI
    freopen("6.8.in","r",stdin);
   // freopen("6.8.out","w",stdout);
    #endif
    #ifdef DEBUGO
    //freopen("6.8.in","r",stdin);
    freopen("6.8.out","w",stdout);
    #endif
    while(read_list(in_order)){
        read_list(post_order);
        build(0,n-1,0,n-1);
        best_sum = 1000000000;
        dfs(post_order[n-1],0);
        cout << best << "\n";
    }
    return 0;
}

之后还要尝试写一下指针版的

例题6-8 Tree Uva548的更多相关文章

  1. 二叉树的递归遍历 Tree UVa548

    题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstre ...

  2. B-tree & B+tree & B*Tree 结构浅析——转

    转自http://www.cnblogs.com/coder2012/p/3330311.html http://blog.sina.com.cn/s/blog_6776884e0100ohvr.ht ...

  3. 【数据结构】K-D Tree

    K-D Tree 这东西是我入坑 ICPC 不久就听说过的数据结构,但是一直没去学 QAQ,终于在昨天去学了它.还是挺好理解的,而且也有用武之地. 目录 简介 建树过程 性质 操作 例题 简介 K-D ...

  4. 点分治——POJ 1741

    写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...

  5. 弦图及其在 OI 中的现代应用

    八月份的时候得知要填综评表格,综评表格里面又需要一个研究性学习报告,而我连研究性学习课的老师长啥样都不知道.于是我把两份 OI 笔记拼拼凑凑成了这篇文章充当两份研究性学习报告之一(另一份可能更有趣一些 ...

  6. 线段tree~讲解+例题

    最近学习了线段树这一重要的数据结构,有些许感触.所以写一篇博客来解释一下线段树,既是对自己学习成果的检验,也希望可以给刚入门线段树的同学们一点点建议. 首先声明一点,本人是个蒟蒻,如果在博客中有什么不 ...

  7. 【例题收藏】◇例题·III◇ 木と整数 / Integers on a Tree

    ◇例题·III◇ 木と整数 / Integers on a Tree 只需要一个美妙的转换,这道题就会变得无比美妙…… 来源:+AtCoder 2148(ARC-063 E)+ ◆ 题目大意 给定一棵 ...

  8. 【日常学习】【二叉树遍历】Uva548 - Tree题解

    这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...

  9. UVA548——Tree(中后序建树+DFS)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

随机推荐

  1. c++ 设计模式8 (Factory Method 工厂方法)

    5. “对象创建”类模式 通过“对象创建”类模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 5.1 工厂方法 动机: ...

  2. Java面向对象设计题2

    有感于很多新人都不知道怎么学习软件开发,个人感觉还是因为练习做的太少,软件开发知识想看懂太难了,必须是边读资料边动手练习.莫说是新人,Java老人研究新技术的时候也是边读资料边练习.因此整理和编排了一 ...

  3. 1.7.4.3 Parsers

    Parsers 除了主查询解析器外,还有一些其他的查询解析器可以使用或者和主查询解析器连合使用.这部分描述了其他查询解析器的细节,并且给出了一些例子: 大多数的解析器都可以使用局部查询参数的方式来表达 ...

  4. FileListEntityProcessor

    一个简单的实体处理程序,可以用于枚举标准文件系统中的文件列表,它不需要使用DataSource.属性如下: fileName:(必填) 用正则表达式来标记文件名 baseDir:(必填) 基础目录,绝 ...

  5. TF-IDF算法扫盲2

    TF-IDF算法是一种简单快捷的文档特征词抽取方法,通过统计文档中的词频来对文档进行主题分类.TF-IDF(term frequency–inverse document frequency)是一种统 ...

  6. ASP.NET 批量更新

    ASP.NET三种常用批量操作: 一.SqlBulkCopy copy = new SqlBulkCopy("");(优先考虑 性能最优) SqlBulkCopy可以将一个Data ...

  7. IOS UItableView 滚动到底 触发事件

    开发过程中,在使用UItableView 总会遇到加载更多的问题,到底是手势响应瀑布流的方法好? 还是添加一个底端cell点击触发加载更多好?我也想有自己的判断.但是我们老板总说了算,没办法,谁叫我给 ...

  8. leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

  9. ASP.NET页面与IIS底层交互和工作原理详解(第一回)

    引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net.他们耐心.细致地告诉你如何一步步拖放控件.设置控件属性.编写CodeBehind代码,以实现某个特定 ...

  10. spring中配置jndi数据源

    spring  AplicationContext.xml中的配置 <bean id="dataSource1" class="org.springframewor ...