zoukankan      html  css  js  c++  java
  • 路径打印(set以及字符串的相关操作)

    题目链接

    题目描述

    给你一串路径,譬如: ac ade bcst d 你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样: a   b     c   d      e b   cst d 同一级的需要按字母顺序排列,不能乱。

    输入描述:

        每个测试案例第一行为一个正整数n(n<=10)表示有n个路径,当n为0时,测试结束,接下来有n行,每行有一个字串表示一个路径,长度小于50。

    输出描述:

    输出目录结构,每一个测试样例的输出紧跟一个空行。
    示例1

    输入

    复制
    4
    ac
    ade
    bcst
    d
    0
    

    输出

    复制
    a
      b
        c
      d
        e
    b
      cst
    d

    题目分析:并没有很好的思路,参考了如下代码

    #include"stdio.h"
    #include<iostream>
    #include<string>
    #include<set>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    typedef long long ll;
    const int maxn=100;
    int n;
    set<string>ss;
    string s;
     
     
    void func(){
    for(int i=0;i<s.length();i++)
        if(s[i]=='\'){
            
            ss.insert(s.substr(0,i));//用分割字符串,并投入一个set,set是不允许重复而且自动排序的
    }
     
     
    }
    int main(){
     
    //    freopen("c://jin.txt","r",stdin);
        while(cin>>n){
            if(n==0)break;
            ss.clear();
            for(int i=0;i<n;i++)
            {    cin>>s;
                if(s[s.length()-1]!='\')s+='\';
                
     
                 func();
                
     
            }
            set<string>::iterator it=ss.begin();
            while(it!=ss.end()){
            s=(*it);
            if(s.find_last_of('\')==string::npos)cout<<s<<endl;
            else{for(int i=0;i<=s.find_last_of('\');i++)//特别注意输出格式
                cout<<' ';
            
            cout<<s.substr(s.find_last_of('\')+1,s.length())<<endl;//substr第二个参数应该是从指定位置开始的长度,但是如果超过了源字符串的长度,就从指定位置到串尾
            
            }
            it++;
            }
            cout<<endl;
        }
     
     
    //    freopen("CON","r",stdin);
    //    system("pause");
     
        return 0;
    }

     代码巧妙地使用了set容器的自动排序功能,将目录一级一级通过string相关操作存储下来并自动排序输出,find_last_of()函数和substr函数的巧妙使用让我学习到了很多

    str.find_last_of("xxx"),找到xxx最后出现的位置

    str.find_last_of(''")=string::npos说明找不到,没有出现过

    str.substr(i,j);i是起始位置,j是结束位置

    分享我的代码:

    由于在一开始就想用map把空格数存储下来,只是不知道该如何提取每一级目录

    #include<iostream>
    #include<map>
    #include<string>
    #include<string.h>
    using namespace std;
    map<string,int> dir;//想用map的键代表目录,值代表空格数
    void fun(string str)
    {
        int flag = 0;
        for(int i=0;i<str.length();i++)
        {
            if(str[i] == '\'){
                dir.insert(pair<string,int>(str.substr(0,i),flag+1));//flag记录的是上一个“”出现的位置,flag+1也就是要输出的空格数
                flag = i;
            }
        }
    }
    int main()
    {
        int n;
        string str;
        
        map<string,int>::iterator it;
    
        while(cin>>n&&n!=0)
        {
            dir.clear();   //清空map
            for(int i=0;i<n;i++)
            {
                cin>>str;
                if(str[str.length()-1] != '\')//如果不是根目录在每个字符串后面添加“”,以便下一步的操作
                    str+='\';
                fun(str);
    
            }
            
            for(it = dir.begin();it!=dir.end();it++)
            {
                if(it->first.find_last_of('\')==string::npos)//根目录
                    cout<<it->first<<endl;
                else{
                    for(int i=0;i<it->second;i++)//输出空格
                        cout<<" ";
                    cout<<it->first.substr(it->first.find_last_of('\')+1,it->first.length())<<endl;
                }
                
                
            }
            cout<<endl;
        }
        return 0;
    }

    /*
    运行时间:51ms

    
    

    占用内存:820k
    */

    题目中还要注意的是格式问题,该题目明确规定“每一个测试样例的输出紧跟一个空行。”,就要在while结束前添加回车!

  • 相关阅读:
    VPC/VM/VBOX安装GHOST版的无法启动系统
    在虚拟机中使用Ghost系统盘安装
    用vmware安装gho文件心得
    VC中Error spawning cl.exe错误的解决方法.
    C语言屏幕打印,再删除打印的内容
    bat根据星期启动程序
    ARP命令详解
    bat生成vbs通过注册表禁用或启用USB端口
    OracleDesigner学习笔记1――安装篇
    MarkMonitor 目前最安全的域名注册商,因此,世界500强网站中的22%域名托管于markmonitor公司
  • 原文地址:https://www.cnblogs.com/ttzz/p/10478935.html
Copyright © 2011-2022 走看看