pku 1057 文件结构问题

#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; string s; struct Directory { string name; vector<Directory> menu; vector<string> files; }; void build(Directory& t, string s) { if(s == "*" || s == "]") return; if(s[0] == 'f') { t.files.push_back(s); cin >> s; build(t, s); } else { Directory d; d.name = s; cin >> s; build(d, s); t.menu.push_back(d); cin >> s; //main part:容易忽略的部分,否则造成构造树的不完全 build(t, s); } } void printLayer(int layer) { for(int i = 0; i < layer; ++i) cout << "| "; } void print(Directory& t, int layer) { printLayer(layer); cout << t.name << endl; for(unsigned int i = 0; i < t.menu.size(); ++i) print(t.menu[i], layer+1); sort(t.files.begin(), t.files.end()); for(unsigned int i = 0; i < t.files.size(); ++i) { printLayer(layer); cout << t.files[i] << endl; } } int main() { string s; int cnt = 0; while(1) { cin >> s; if(s == "#") break; Directory d; d.name = "ROOT"; printf("DATA SET %d:/n", ++cnt); build(d, s); print(d, 0); cout << endl; } return 0; } //主要使用递归思想(类似于树的建立和遍历过程) //这里主要是借鉴的利用vector来建立多分支树的方法

你可能感兴趣的:(String,vector,Build,include,layer,stdstring)