548 - Tree (UVA)

题目链接如下:

Online Judge

开始的代码如下:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
const int INF = 99999999;
// #define debug

std::vector<int> inorder, postorder;
std::string line;
int t, root, minn, key, sum;
std::map<int, int> inloc, left, right;

int findRoot(int inLeft, int inRight, int postLeft, int postRight){
    if (inLeft == inRight){
        return 0;
    }
    int rt = postorder[postRight - 1];
    int loc = inloc[rt];
    left[rt] = findRoot(inLeft, loc, postLeft, loc - inLeft + postLeft);
    right[rt] = findRoot(loc + 1, inRight, loc - inLeft + postLeft, postRight - 1);
    return rt;
}

void dfs(int k){
    sum += k;
    if (!left[k] && !right[k]){
        if (sum < minn){
            minn = sum;
            key = k;
        } else if (sum == minn && key > k){
            key = k;
        }
        sum -= k;
        return;
    }
    if (left[k]){
        dfs(left[k]);
    }
    if (right[k]){
        dfs(right[k]);
    }
    sum -= k;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, line)){
        std::stringstream in(line);
        while (in >> t){
            inloc[t] = inorder.size();
            inorder.push_back(t);
        }
        getline(std::cin, line);
        std::stringstream post(line);
        while (post >> t){
            postorder.push_back(t);
        }
        root = findRoot(0, inorder.size(), 0, inorder.size());
        minn = INF;
        sum = 0;
        dfs(root);
        printf("%d\n", key);
        inorder.clear();
        postorder.clear();
        left.clear();
        right.clear();
        inloc.clear();
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

后来看别人的代码发现dfs可以省掉,修改后如下:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
const int INF = 99999999;
// #define debug

std::vector<int> inorder, postorder;
std::string line;
int t, root, minn, key;
std::map<int, int> inloc, left, right;

int findRoot(int inLeft, int inRight, int postLeft, int postRight, int tot){
    if (inLeft == inRight){
        return 0;
    }
    int rt = postorder[postRight - 1];
    int sum = tot + rt;
    int loc = inloc[rt];
    left[rt] = findRoot(inLeft, loc, postLeft, loc - inLeft + postLeft, sum);
    right[rt] = findRoot(loc + 1, inRight, loc - inLeft + postLeft, postRight - 1, sum);
    if (!left[rt] && !right[rt]){
        if (sum < minn || (sum == minn && rt < key)){
            minn = sum;
            key = rt;
        }
    }
    return rt;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, line)){
        std::stringstream in(line);
        while (in >> t){
            inloc[t] = inorder.size();
            inorder.push_back(t);
        }
        getline(std::cin, line);
        std::stringstream post(line);
        while (post >> t){
            postorder.push_back(t);
        }
        minn = INF;
        root = findRoot(0, inorder.size(), 0, inorder.size(), 0);
        printf("%d\n", key);
        inorder.clear();
        postorder.clear();
        left.clear();
        right.clear();
        inloc.clear();
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值