7-30 目录树 (30 分)

题目链接

输入:
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值