#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
struct node {
node (string _name, int _sz): name(_name), sz(_sz) {};
string name;
int sz;
node *next[25];
int k;
};
node *root;
void dfs(node *nd, string pre) {
int i, k;
cout << "_";
if (nd->name[0] != '*') {
cout << nd->name << "[" << nd->sz << "]" << endl;
} else {
cout << nd->name << "[" << nd->sz << "]" << endl;
k = nd->k;
for (i = 0; i < k; i++) {
cout << pre << " |";
if (i != k - 1) {
dfs(nd->next[i], pre + " |");
} else {
dfs(nd->next[i], pre + " ");
}
}
}
}
int update(node *nd) {
if (nd->name[0] != '*') {
return nd->sz;
} else {
int sum = nd->sz;
for (int i = 0; i < nd->k; i++) {
sum += update(nd->next[i]);
}
return nd->sz = sum;
}
}
void push_down(string name, int sz, int &k, node *nd, queue<node*> &q) {
nd->next[k] = new node(name, sz);
if (name[0] == '*')q.push(nd->next[k]);
k++;
}
int main() {
string name, s1, s2;
int sz, files, k;
node *nd;
while (cin >> name >> sz) {
root = new node(name, sz);
queue<node*> q;
q.push(root);
while (!q.empty()) {
files = q.size();
while (files--) {
k = 0;
nd = q.front();
q.pop();
while (cin >> s1) {
if (s1 == "()")break;
cin >> s2;
if (s1[0] == '(') s1 = s1.substr(1, s1.size() - 1);
if (s2[s2.size() - 1] == ')') {
s2 = s2.substr(0, s2.size() - 1);
push_down(s1, stoi(s2), k, nd, q);
break;
}
push_down(s1, stoi(s2), k, nd, q);
}
nd->k = k;
}
}
update(root);
printf("|");
dfs(root, " ");
}
}
zoj 1635 Directory Listing
于 2022-08-08 15:19:39 首次发布
本文详细介绍了如何解决ZOJ 1635题目,重点探讨了使用深度优先搜索(DFS)策略进行目录列举的方法。通过实例分析,解释了算法的实现步骤和关键代码,帮助读者理解如何有效地遍历和处理目录结构。
摘要由CSDN通过智能技术生成