2018_2_14_Disk Tree_层次结构

16 篇文章 0 订阅
5 篇文章 0 订阅
g++
Disk Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2576 Accepted: 939

Description

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 of the input file contains single integer number N (1 <= N <= 500) that denotes a total number of distinct directory paths. 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

Write to the output file 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.

Sample Input

7
WINNT\SYSTEM32\CONFIG
GAMES
WINNT\DRIVERS
HOME
WIN\SOFT
GAMES\DRIVERS
WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86

Sample Output

GAMES
 DRIVERS
HOME
WIN
 SOFT
WINNT
 DRIVERS
 SYSTEM32
  CERTSRV
   CERTCO~1
    X86
  CONFIG

Source

Northeastern Europe 2000
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue> 
#include<string>
#include<map>

using namespace std; 

typedef map<string,int> msi;
const int N=2e4;
msi ms[N];

int tot;

void print(int m,int s){
	for(msi::iterator it=ms[m].begin();it!=ms[m].end();it++){
		for(int i=0;i<s;i++)
		putchar(' ');
		cout<<it->first<<endl;
		print(it->second,s+1);
	}
}

string t,s;

int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		int now=0,pos=0,p=0;
		cin>>t;
		while((p=t.find('\\',p))!=-1){
			s=t.substr(pos,p-pos);
			if(!ms[now].count(s))ms[now][s]=++tot;
			now=ms[now][s];
			pos=++p;
		}
		s=t.substr(pos);
		if(!ms[now].count(s))ms[now][s]=++tot;
	}
	print(0,0);
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值