2020-winter-7-3 File Path
作者:
Kue
,
2021-03-06 17:15:36
,
所有人可见
,
阅读 307
模拟文件路径
满分代码
/**
模拟文件夹
用什么数据结构存储呢?
1 并查集
难点: 空格的处理
vector<int> vc 存每一层的文件
用p[N] 存上一级
*/
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1010, M = 10010;;
vector<int> vc[N];
int p[M];
int main()
{
int m, q, root;
memset(p, -1, sizeof p);
cin >> m;
getchar();
while (m --)
{
string str;
int cnt = 0;
getline(cin, str);
while (str[cnt] == ' ') cnt++;
int x = stoi(str.substr(cnt));
vc[cnt].push_back(x);
if (cnt == 0) p[x] = x, root = x;
else
{
int size = vc[cnt - 1].size();
p[x] = vc[cnt - 1][size - 1];
}
}
cin >> q;
while (q --)
{
int x;
cin >> x;
vector<int> res;
if (p[x] == -1) printf("Error: %04d is not found.\n", x);
else
{
for(int i = x; i != p[i]; i = p[i]) res.push_back(i);
res.push_back(root);
for(int i = res.size() - 1; i >= 0; i--)
{
printf("%04d", res[i]);
if (i) printf("->");
}
printf("\n");
}
}
}