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 (≤10^3), 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.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cctype>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <sstream>
using namespace std;
#pragma warning(disable:4996)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define repr(i, a, b) for(int i = a; i >= (b); --i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
const int maxn = 10005;
const int INF = 0x3f3f3f3f;
int n, m;
vector<int> G[maxn];
vector<int> tmpPath, resPath;
void dfs(int src, int dst) {
if (src == dst) {
tmpPath.push_back(dst);
resPath = tmpPath;
tmpPath.pop_back();
return;
}
tmpPath.push_back(src);
for (int i = 0; i < G[src].size(); i++) {
dfs(G[src][i], dst);
}
tmpPath.pop_back();
}
int main() {
cin >> n;
getchar();
int prek, preu, times = 2;
vector<int> ls(n);
set<int> st;
rep(i, 0, n) {
string str;
getline(cin, str);
int nums = 0, v = stoi(str);
st.insert(v);
if (i == 0) {
ls[0] = v;
continue;
}
for (int j = 0; j < str.size() && str[j] == ' '; j++)
nums++;
ls[nums] = v;
G[ls[nums - 1]].push_back(v);
}
cin >> m;
rep(i, 0, m) {
int query;
cin >> query;
if (st.count(query) == 0) {
printf("Error: %04d is not found.\n", query);
continue;
}
resPath.clear();
dfs(ls[0], query);
rep(j, 0, sz(resPath)) {
if (j != 0)
printf("->");
printf("%04d", resPath[j]);
}
printf("\n");
}
return 0;
}