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

问题思路:

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

代码实现:

cpp 复制代码
#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;
}
相关推荐
眼镜哥(with glasses)几秒前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
zh_xuan3 小时前
c++ 单例模式
开发语言·c++·单例模式
int型码农5 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
利刃大大5 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
UFIT5 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面5 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked935 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,5 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵5 小时前
有效的括号题解
数据结构·算法·
GIS小天5 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票