zoukankan      html  css  js  c++  java
  • 7-30 目录树 (30分)

    在ZIP归档文件中,保留着所有压缩文件和目录的相对路径和名称。当使用WinZIP等GUI软件打开ZIP归档文件时,可以从这些信息中重建目录的树状结构。请编写程序实现目录的树状结构的重建工作。

    输入格式:

    输入首先给出正整数N(≤),表示ZIP归档文件中的文件和目录的数量。随后N行,每行有如下格式的文件或目录的相对路径和名称(每行不超过260个字符):

    • 路径和名称中的字符仅包括英文字母(区分大小写);
    • 符号“”仅作为路径分隔符出现;
    • 目录以符号“”结束;
    • 不存在重复的输入项目;
    • 整个输入大小不超过2MB。

    输出格式:

    假设所有的路径都相对于root目录。从root目录开始,在输出时每个目录首先输出自己的名字,然后以字典序输出所有子目录,然后以字典序输出所有文件。注意,在输出时,应根据目录的相对关系使用空格进行缩进,每级目录或文件比上一级多缩进2个空格。

    输入样例:

    7
    b
    c
    abcd
    ac
    abd
    ada
    adz
    
     

    输出样例:

    root
      a
        d
          z
          a
        bc
      ab
        cd
        d
      c
      b


    建树,结点包含文件夹名字以及子文件夹数组和文件数组,按照要求输出。
    代码:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    struct cata {
        char name[300];
        vector<cata> sub;
        vector<string> file;
        cata(){} 
        cata(char *name) {
            strcpy(this -> name,name);
        }
        cata *find1(char *subname) {
            for(int i = 0;i < sub.size();i ++) {
                if(!strcmp(sub[i].name,subname)) return &sub[i];
            }
            return NULL;
        }
        bool find2(char *filename) {
            for(int i = 0;i < file.size();i ++) {
                if(!strcmp(file[i].c_str(),filename)) return true;
            }
            return false;
        }
    }root,*l,*temp;
    int n,c;
    char s[300],ch;
    void print(const char *t,int h) {
        for(int i = 0;i < h;i ++) printf("  ");
        printf("%s
    ",t);
    }
    bool cmp(cata &a,cata &b) {
        return strcmp(a.name,b.name) < 0;
    }
    void dfs(cata node,int h) {
        for(int i = 0;i < h;i ++) printf("  ");
        printf("%s
    ",node.name);
        sort(node.sub.begin(),node.sub.end(),cmp);
        for(int i = 0;i < node.sub.size();i ++) {
            dfs(node.sub[i],h + 1);
        }
        sort(node.file.begin(),node.file.end());
        for(int i = 0;i < node.file.size();i ++) {
            print(node.file[i].c_str(),h + 1);
        }
    }
    int main() {
        scanf("%d",&n);
        strcpy(root.name,"root");
        getchar();
        for(int i = 0;i < n;i ++) {
            l = &root;
            while((ch = getchar()) != '
    ') {
                if(ch == '\') {
                    s[c] = 0;
                    c = 0;
                    temp = l -> find1(s);
                    if(!temp) l -> sub.push_back(cata(s)),temp = &*(l -> sub.rbegin());
                    l = temp;
                }
                else s[c ++] = ch;
            }
            if(c) {
                s[c] = 0;
                c = 0;
                if(!l -> find2(s)) l -> file.push_back(s);
            }
        }
        dfs(root,0);
    }
  • 相关阅读:
    阿里播放器踩坑记录 进度条重构 video loadByUrl失效解决方案
    liunx 安装nc/netcat centos安装netcat
    jquery实现显示textarea输入字符数
    SQL 时间戳转换为日期
    .Net WebRequest异步请求与WebClient异步请求
    SQL删除多列语句
    jQuery为元素设置css的问题
    关于调试WCF时引发的异常XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side
    SQL删除指定条件的重复数据,只保留一条
    net.exe use命令的使用
  • 原文地址:https://www.cnblogs.com/8023spz/p/12300688.html
Copyright © 2011-2022 走看看