【PAT甲级】1178 File Path 测试点1

The figure shows the tree view of directories in Windows File Explorer. When a file is selected, there is a file path shown in the above navigation bar. Now given a tree view of directories, your job is to print the file path for any selected file.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤103), which is the total number of directories and files. Then N lines follow, each gives the unique 4-digit ID of a file or a directory, starting from the unique root ID 0000. The format is that the files of depth d will have their IDs indented by d spaces. It is guaranteed that there is no conflict in this tree structure.

Then a positive integer K (≤100) is given, followed by K queries of IDs.

Output Specification:

For each queried ID, print in a line the corresponding path from the root to the file in the format: 0000->ID1->ID2->...->ID. If the ID is not in the tree, print Error: ID is not found. instead.

Sample Input:

14
0000
 1234
  2234
   3234
    4234
    4235
    2333
   5234
   6234
    7234
     9999
  0001
   8234
 0002
4 9999 8234 0002 6666

Sample Output:

0000->1234->2234->6234->7234->9999
0000->1234->0001->8234
0000->0002
Error: 6666 is not found.

1. 输入数据中的空格数代表层数

2. 每次输入都要记录更新每层数最后一个节点的值

3. 根据每个文件的层数创建一棵树保存在vector file

4. 使用dfs搜索并记录下目标的路径

测试点1:当查找的目标为0000

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;

bool vis[10005];
vector<int> file[10005],out;
void dfs(int current, int target, vector<int>& path) {
	path.push_back(current);
	if (current == target) {
		out = path; return;
	}
	for (int i = 0; i < file[current].size(); i++) {
		dfs(file[current][i], target, path);
	}
	path.pop_back();
}
int main() {
	int n;
	scanf("%d", &n); cin.ignore();
	string temp;
	int preLevel[10001]; //记录树的每层最后一个节点值
	preLevel[0] = 0;
	for (int i = 0; i < n; i++) {
		getline(cin, temp);
		if (i == 0) continue;
		int level = count(temp.begin(), temp.end(), ' '); //记录空格数,表示层数
		int id = atoi(temp.substr(temp.find_first_not_of(' ')).c_str());
		vis[id] = true; //是否存在某个文件夹名
		preLevel[level] = id; //更新每层最后一个节点值
		file[preLevel[level - 1]].push_back(id); //将文件保存在上级路径中
	}
	int k;
	scanf("%d", &k); cin.ignore();
	for (int i = 0; i < k; i++) {
		string query; cin >> query;
		int q = atoi(query.c_str());
		if (q == 0) { //测试点1
			printf("0000\n"); continue;
		}
		if (!vis[q]) {
			printf("Error: %s is not found.\n", query.c_str());
		}else {
			vector<int> path;
			out.clear();
			dfs(0, q, path);
			printf("0000");
			for (int j = 1; j < out.size(); j++) {
				printf("->%04d", out[j]);
			}
			printf("\n");
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值