例题6-8 Tree Uva548

这道题我一直尝试用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 ",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 ",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* ",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 << " ";
    }
    return 0;
}

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

原文地址:https://www.cnblogs.com/Wade-/p/5756223.html