OJ_1090 路径打印

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
void func()
{
      int n;
    string str;
    vector<string> v;
    while(cin>>n){
                  if(n==0)break;
        v.resize(0);
        while(n--){
            cin>>str;
            
            if(str[str.length()-1]=='\\'){
                str.erase(str.length()-1,str.length());
            }
            if(find(v.begin(),v.end(),str)==v.end()){
                v.push_back(str);
            }
            while(str.find('\\')!=string::npos){
                str.erase(str.find_last_of('\\'),str.length());
                if(find(v.begin(),v.end(),str)==v.end()){
                    v.push_back(str);
                }
            }
        }
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();i++){
            if(v[i].find('\\')==string::npos){
                cout<<v[i]<<endl;
            }
            else{
                for(int j=0;j<v[i].find_last_of('\\');j++){
                    cout<<" ";
                }
                cout<<" ";
                cout<<v[i].substr(v[i].find_last_of('\\')+1,v[i].length());
                cout<<endl;
            }
        }
        cout<<endl;
        }
}
int main(int argc, char *argv[])
{
    
	//printf("Hello, world\n");
	func();
	return 0;
}

将每个子路径保存,然后排序,再按格式输出

题目描述:

给你一串路径,譬如:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值