题目链接
输入:
10
b
a\bc
a\bc\
ab\d
a\d\a
a\d\z
a\d\z
a\d\z\
a\d\z\
a\d\z\e
输出:
root
a
bc
d
z
e
a
z
bc
ab
d
b
#include<iostream>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;
struct cmp {//类比较器
bool operator()(const string& a, const string& b) {//重载()
if(!(a[a.size()-1] == '\\' ^ b[b.size()-1] == '\\')){
return a < b; //同时有或同时没有'\'
} else if(a[a.size()-1] == '\\') return true;
else return false;//false就是a<b不成立
}
};
typedef set<string,cmp> Heap;
unordered_map<string,Heap> content;
string HandleStr(string name){
string t;
for(int i = name.size()-1; i >= 0; i--){
if(name[i] == '\\' && t.size()) break;
else if(name[i] != '\\') t = name[i] + t;
}
return t;
}
void fun(string name,int num){//num为第几层
for(int i = 0; i < num*2; i++) cout<<' ';
cout<<HandleStr(name)<<'\n';
for(string i:content[name]){
fun(i,num+1);
}
}
int main(){
int n;
string str,parents;
cin>>n;
for(int i = 0; i < n; i++){
string tmp = "";
parents = "root";
cin>>str;
for(int i = 0; i < str.size(); i++){
tmp += str[i];
if(str[i] == '\\'){
content[parents].insert(tmp);
parents = tmp;
}
}
if(tmp.size() && tmp[tmp.size()-1]!='\\'){//最后一个字符不是'\'就是文件
content[parents].insert(tmp);
}
}
fun("root",0);
return 0;
}