Source: http://noi.openjudge.cn/ch0202/1777/ 1777: Document Structure "map"
Total time limit: 1000ms memory limit: 65536kB
Description
It is often useful to see the structure of a file system on your computer. This is an example of the "explorer" program above Microsoft windows. But before there is a graphical interface, there is no graphical representation, and the best way to do that is to show the structure of the directory and the file as a "graph", and use the indented form to represent the structure of the directory. Like what:
ROOT
| Dir1
| File1
| File2
| File3
| Dir2
| Dir3
| File1
File1
File2
This diagram illustrates: The root directory consists of three subdirectories and two files. The first subdirectory contains 3 files, the second subdirectory is empty, and the third subdirectory contains a file.
input
Your task is to write a program to read some test data. Each set of test data represents the file structure of a computer. Each set of test data ends with ' * ', and all reasonable input data ends with ' # '. A set of test data includes the names of some files and directories (although we do not give them in the input, we always assume that the root directory is the outermost directory). In the input, the end of the contents of a directory is represented by '] '. The first letter of the directory name is ' d ', and the first letter of the file name is ' F '. Filenames may or may not have extensions (such as Fmyfile.dat and Fmyfile). No spaces are included in the name of the file or directory, and the length is no more than 30. The sum of the number of subdirectories and the number of files in a directory does not exceed 30.
Output
When displaying content in a directory, the subdirectories (if any) are displayed first, and then the file (if any) is displayed. The file requirements are displayed in the alphabetical order of the first name (the table of contents does not appear in alphabetical order by name, only as shown in the table of contents). For each set of test data, we will first output "data SETx:", where x is the number of the test data (starting at 1). Separate the two sets of test data by outputting a blank line.
What you need to note is that we use a ' | ' and 5 empty glyd indicate the level of indentation.
Sample Input
File1
File2
Dir3
Dir2
File1
File2
]
]
File4
Dir1
]
File3
*
File2
File1
*
#
Sample Output
DATA SET 1:
ROOT
| Dir3
| | Dir2
| | File1
| | File2
| Dir1
File1
File2
File3
File4
DATA SET 2:
ROOT
File1
File2
Tips
A directory and its subdirectories are at different levels
A directory and the files inside it are at the same level
-----------------------------------------------------
Thinking of solving problems
Using stacks to represent the depth and exit of a directory
Again: C + + to use stack words vector analogy stack class useful, with the vector class on the line
-----------------------------------------------------
Code
#include <fstream> #include <iostream> #include <algorithm> #include <vector> #include <
String> using namespace std;
int main () {#ifndef Online_judge ifstream fin ("xly2016D.txt");
if (!fin) {exit (1);
} string buf;
Vector<string>::iterator it; vector< vector<string> > Fstack; Use the stack to characterize the deepening and exit of the directory vector<string> fbuf;
File name temporary storage array int n_dataset = 1;
int n_tab = 0;
int i = 0, j = 0;
Fin >> buf; while (buf! = "#") {if (Fstack.empty () && fbuf.empty ())//If the stack and file list are empty, a new dataset {cout <& is opened Lt
"DATA SET" << n_dataset << ":" << Endl;
cout << "ROOT" << Endl;
} if (buf = = "") {continue;
} if (buf.at (0) = = ' d ') {n_tab++; if (n_tab>0) {for (i=0; i<n_tab; i++) {buf = "|
"+ buf; }} cout << buf << Endl; Direct Output Directory name Fstack.push_back (FBUF);The old file list is saved into the stack fbuf.clear (); Go to the new level directory, empty the file name list to record the file in the new directory} else if (buf.at (0) = = ' F ') {if (n_tab>0) {f or (i=0; i<n_tab; i++) {buf = "|
"+ buf; }} fbuf.push_back (BUF); Add the file name to the list of filenames} else if (buf== "]") {sort (Fbuf.begin (), Fbuf.end ()); Sort the file names in dictionary order for (It=fbuf.begin (); It!=fbuf.end (); it++) {cout << (*it) << Endl; Output all files under this level directory in dictionary order} fbuf = Fstack.back (); Returns the list of file names in the upper directory Fstack.pop_back ();
Pop up the file list n_tab--; } else if (buf== "*") {sort (Fbuf.begin (), Fbuf.end ()); Sort the file names in dictionary order for (It=fbuf.begin (); It!=fbuf.end (); it++) {cout << (*it) << Endl; Output all files under this level directory in Dictionary order} fbuf.clear (); Empty file list fstack.clear ();
Empty file list stack cout << Endl;
n_dataset++;
N_tab = 0;
} fin >> buf;
} Fin.close ();
return 0;
#endif//LOCAL FILE #ifdef Online_judge string buf;
Vector<string>::iterator it; vector< vector<string> > Fstack; Use the stack to characterize the deepening and exit of the directory vector<string> fbuf;
File name temporary storage array int n_dataset = 1;
int n_tab = 0;
int i = 0, j = 0;
Cin >> BUF; while (buf! = "#") {if (Fstack.empty () && fbuf.empty ())//If the stack and file list are empty, a new dataset {cout <& is opened Lt
"DATA SET" << n_dataset << ":" << Endl;
cout << "ROOT" << Endl;
} if (buf = = "") {continue;
} if (buf.at (0) = = ' d ') {n_tab++; if (n_tab>0) {for (i=0; i<n_tab; i++) {buf = "|
"+ buf; }} cout << buf << Endl; Direct Output Directory name Fstack.push_back (FBUF); The old file list is saved into the stack fbuf.clear (); Go to the new level directory, empty the file name list to record the file in the new directory} else if (buf.at (0) = = ' F ') {if (n_tab>0) {f or (i=0; i< n_tab; i++) {buf = "|
"+ buf; }} fbuf.push_back (BUF); Add the file name to the list of filenames} else if (buf== "]") {sort (Fbuf.begin (), Fbuf.end ()); Sort the file names in dictionary order for (It=fbuf.begin (); It!=fbuf.end (); it++) {cout << (*it) << Endl; Output all files under this level directory in dictionary order} fbuf = Fstack.back (); Returns the list of file names in the upper directory Fstack.pop_back ();
Pop up the file list n_tab--; } else if (buf== "*") {sort (Fbuf.begin (), Fbuf.end ()); Sort the file names in dictionary order for (It=fbuf.begin (); It!=fbuf.end (); it++) {cout << (*it) << Endl; Output all files under this level directory in Dictionary order} fbuf.clear (); Empty file list fstack.clear ();
Empty file list stack cout << Endl;
n_dataset++;
N_tab = 0;
} cin >> buf;
} return 0; #endif}