1 Disk Tree
- Disk Tree
Time limit: 2.0 second
Memory limit: 64 MB
Hacker Bill has accidentally lost all the information from his workstation’s hard drive and he has no backup copies of its contents. He does not regret for the loss of the files themselves, but for the very nice and convenient directory structure that he had created and cherished during years of work.
Fortunately, Bill has several copies of directory listings from his hard drive. Using those listings he was able to recover full paths (like “WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86”) for some directories. He put all of them in a file by writing each path he has found on a separate line.
Your task is to write a program that will help Bill to restore his state of the art directory structure by providing nicely formatted directory tree.
Input
The first line contains single integer N that denotes a total number of distinct directory paths (1 ≤ N ≤ 500). Then N lines with directory paths follow. Each directory path occupies a single line and does not contain any spaces, including leading or trailing ones. No path exceeds 80 characters. Each path is listed once and consists of a number of directory names separated by a back slash (“”).
Each directory name consists of 1 to 8 uppercase letters, numbers, or the special characters from the following list: exclamation mark, number sign, dollar sign, percent sign, ampersand, apostrophe, opening and closing parenthesis, hyphen sign, commercial at, circumflex accent, underscore, grave accent, opening and closing curly bracket, and tilde (“!#$%&'()-@^_`{}~”).
Output
Output the formatted directory tree. Each directory name shall be listed on its own line preceded by a number of spaces that indicate its depth in the directory hierarchy. The subdirectories shall be listed in lexicographic order immediately after their parent directories preceded by one more space than their parent directory. Top level directories shall have no spaces printed before their names and shall be listed in lexicographic order. See sample below for clarification of the output format.
C++实现
/* 1067. Disk Tree - http://acm.timus.ru/problem.aspx?num=1067
*
* Strategy:
* Straightforward; the code serves as its own comments.
*
* Performance:
* Linearithmic in the size of the input, runs in 0.062s using 4,540KB memory.
*/
#include <iostream>
#include <map>
#include <sstream>
#include <string>
struct Dir
{
std::map<std::string, Dir*> subs;
} dirs[50001];
int p = 1;
Dir* addDir(Dir* dir, std::string str)
{
auto& d = dir->subs[str];
if(!d)
d = &dirs[p++];
return d;
}
void print(Dir* dir, int depth = 0)
{
for(auto s : dir->subs)
{
for(int i = 0; i < depth; i++)
std::cout << " ";
std::cout << s.first << "\n";
print(s.second, depth+1);
}
}
int main()
{
int N;
std::cin >> N;
for(int i = 0; i < N; i++)
{
std::string str, dirstr;
std::cin >> str;
std::stringstream ss(str);
Dir* dir = &dirs[0];
while (std::getline(ss, dirstr, '\\'))
dir = addDir(dir, dirstr);
}
std::stringstream out;
print(&dirs[0]);
}
Java实现 - 使用Map来操作
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
DiskTree head,diskTree;
head = new DiskTree();
DiskTree.diskTreeList[0] = head;
for (int i = 0 ; i < N; i++){
diskTree = head ;
String dirStr = in.next();
String[] dirs = dirStr.split("\\\\");
for (String dir : dirs) {
//System.out.println(dir);
diskTree = head.add(dir,diskTree);
}
// System.out.println(dirStr);
}
head.print(DiskTree.diskTreeList[0],0);
}
}
class DiskTree {
public static DiskTree[] diskTreeList = new DiskTree[50001];
Map<String,DiskTree> hashMap = new HashMap<String,DiskTree>();
public static int p = 1;
public DiskTree add(String dirName , DiskTree diskTree){
if (!diskTree.hashMap.containsKey(dirName)){
DiskTree newDiskTree = new DiskTree();
diskTree.hashMap.put(dirName,newDiskTree);
newDiskTree = diskTree.hashMap.get(dirName);
//diskTree.hashMap.put(dirName,newDiskTree);
diskTreeList[p++] = newDiskTree;
return newDiskTree;
}else{
DiskTree subDiskTree = diskTree.hashMap.get(dirName);
return subDiskTree;
}
}
public static void print(DiskTree dir, int depth){
for(Map.Entry<String, DiskTree> entry : dir.hashMap.entrySet())
{
for (int i=0; i<depth;i++){
System.out.print(" ");
}
System.out.println(entry.getKey());
print(entry.getValue(),depth+1);
}
}
}