#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=11;
string blank(int n){ // 返回长度为n的、由空格组成的字符串
string res="";
while(n--)
res+=" ";
return res;
}
int main() {
int n;
while(cin>>n && n) {
string arrStr[MAXN];
for(int i=0; i<n; i++)
cin>>arrStr[i];
sort(arrStr,arrStr+n); // 对路径进行排序
vector<string> path[MAXN];
for(int i=0; i<n; i++) {
string temp=arrStr[i];
while(temp.size()>0) { // 以'\\'为分隔符,把路径中的子目录进行拆分,放在path里
int pos = temp.find('\\');
if(pos==string::npos){
path[i].push_back(temp);
temp = "";
}else{
path[i].push_back(temp.substr(0,pos));
temp = temp.substr(pos+1);
}
}
if(i==0){ // 输出第1个路径对应的树状图
for(int j=0;j<path[i].size();j++){
cout<<blank(j*2)+path[i][j]<<endl;
}
}else{ // 输出第1个以外的路径对应的树状图
int pos=0;
while(pos<path[i].size()){ // 仅跟第i-1个个路径比较
if(pos>=path[i-1].size()) break;
if(path[i][pos]!=path[i-1][pos]) break;
pos++;
}
while(pos<path[i].size()){
cout<<blank(pos*2)+path[i][pos]<<endl;
pos++;
}
}
}
cout<<endl; // 每一个测试样例的输出紧跟一个空行
}
return 0;
}
题目——路径打印
最新推荐文章于 2022-12-06 10:08:18 发布