给你一串路径,譬如:
a\b\c
a\d\e
b\cst
d\
你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录的首字符向右缩两个空格,就像这样:
a
b
c
d
e
b
cst
d
注:同一级的需要按字母顺序排列,不能乱。
每个测试案例第一行为一个正整数n(n<=10)表示有n个路径,当n为0时,测试结束,接下来有n行,每行有一个字串表示一个路径,长度小于50。
输出目录结构,每一个测试样例的输出紧跟一个空行。
4 a\b\c a\d\e b\cst d\ 0
a
b
c
d
e
b
cst
d
# -*- coding: utf-8 -*-
def print_path(path_words, indent_word=0):
for i in range(len(path_words)):
if i < indent_word:
continue
else:
if path_words[i] == '':
continue
print(i*" " + path_words[i])
while True:
try:
n = int(input())
if n == 0:
break
# 注意,要将目录排序
path_list = sorted([input() for i in range(n)])
path_list = [path.split('\\') for path in path_list]
for idx, path in enumerate(path_list):
if idx == 0:
print_path(path)
else:
# 判断上一个目录与当前目录有几个相同
before_path = path_list[idx-1]
for i in range(len(before_path)):
if before_path[i] == path[i]:
continue
else:
print_path(path, indent_word=i)
# 走进当前else说明已经有不匹配目录,停止后面的目录匹配检测
break
print()
except Exception as ex:
break
while True:
try:
num = int(input())
if not num:
break
trees = []
for i in range(num):
trees.append(input().strip('\\').split('\\'))
trees.sort() #先把树进行排序
#初始化一个未在目录出现的字符当做上一个目录
lastSign = ['#','#']
for i in range(num):
beginIndex = 0 #该目录开始输出的子目录索引
blackNum = 0 #输出空格数
while beginIndex < len(trees[i]) and beginIndex < len(lastSign) and trees[i][beginIndex] == lastSign[beginIndex]:
blackNum += 2 #每增加一层加两个索引
beginIndex += 1
for j in range(beginIndex, len(trees[i])):
print(" "*blackNum + trees[i][j])
blackNum += 2
lastSign = trees[i]
print()
except Exception:
break