题目——路径打印

题目链接

#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;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值