【PAT甲级】1178 File Path(25分)[文件树,模拟,unordered_map]

 

问题思路:

  • 在不断输入的过程中,可以通过层深和一个二维的vector数组来建立一棵树。
  • 即每输入一个节点,应当作为该节点上一层的最后一个节点的子孩子。
  • 用一个哈希value来指定节点的id,通过一个last记录每个节点id(此时作为last的下标)的父节点是谁。
  • 事实上,这种溯源的思路在之前的这道题中也有体现,只不过那里是图,这里是树。【PAT甲级】1175 Professional Ability Test(30分)[dijkstra(优先队列),拓扑排序]

代码实现:

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

int n;
vector<string> last(1005);
unordered_map<string, int> value;

void buildTree() {
    vector<vector<string>> Tree(1005, vector<string>());    // 每一个节点作为每一层深的最后一个节点的子孩子
    int cnt = 0;
    cin >> n;
    cin.ignore(); // Clear the newline character from the input buffer

    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);
        int depth = line.find_first_not_of(' '); // 层深
        string id = line.substr(depth, 4); // 节点字符串
        value[id] = cnt;
        Tree[depth].push_back(id);
        if (depth > 0) {
            last[cnt] = Tree[depth - 1].back();    // 每个节点记录上一层的父节点是谁
        }
        cnt += 1;
    }
    return ;
}

vector<string> findPath(const string& target) {
    vector<string> path;
    if (value.find(target)  == value.end()) return path;
    string now = target;
    while (now != "0000") {
        path.push_back(now);
        now = last[value[now]];
    }
    path.push_back("0000");
    return path;
}

int main() {
    buildTree();
    int k;
    cin >> k;

    for (int i = 0; i < k; i++) {
        string query;
        cin >> query;
        vector<string> path = findPath(query);
        if (!path.empty()) {
            for (int i = path.size() - 1; i >= 0; i--) {
                cout << path[i];
                i && printf("->");
            }
            printf("\n");
        } else {
            cout << "Error: " << query << " is not found." << endl;
        }
    }

    return 0;
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值