zoukankan      html  css  js  c++  java
  • 九度oj 题目1090:路径打印

    题目描述:

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

    输入:

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

    输出:

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

    样例输入:
    4
    ac
    ade
    bcst
    d
    0
    样例输出:
    a
      b
        c
      d
        e
    b
      cst
    d

    这道题我用了比较复杂的数据结构,但题目的输出说的不是很明确,代码如下
      1 #include <cstdio>
      2 #include <cstdlib>
      3 #include <cstring>
      4 #include <string>
      5 #include <cmath>
      6 #include <algorithm>
      7 #include <queue>
      8 #define inf 0x3f3f3f3f
      9 #define LenM 52
     10 #define ChiM 12
     11 using namespace std;
     12 
     13 struct Node
     14 {
     15     char name[LenM];
     16     int level;
     17     int childCount;
     18     Node *child[ChiM];
     19 };
     20 
     21 Node root;
     22 char temp[LenM];
     23 char temp2[LenM];
     24 
     25 int cmp(const void *a, const void *b) {
     26     Node *at = *(Node**)a;
     27     Node *bt = *(Node**)b;
     28     return strcmp(at->name,bt->name);
     29 }
     30 Node *add(Node *now, char toAdd[]) {
     31     bool isFind = false;
     32     int findNum = -1;
     33     for(int i = 0; i < now->childCount; i++) {
     34         if(strcmp(toAdd, (now->child[i])->name) == 0) {
     35             isFind = true;
     36             findNum = i;
     37             break;
     38         }
     39     }
     40     if(!isFind) {
     41         Node *childNode = new Node;
     42         childNode->level = now->level + 1;
     43         strcpy(childNode->name, toAdd);
     44         childNode->name[strlen(toAdd)] = '';
     45         childNode->childCount = 0;
     46         for(int i = 0; i < ChiM; i++) {
     47             childNode->child[i] = NULL;
     48         }
     49         now->child[now->childCount] = childNode;
     50         (now->childCount)++;
     51         return childNode;
     52     }
     53     else {
     54         return now->child[findNum];
     55     }
     56 }
     57 
     58 void nSort(Node *now) {
     59     qsort(now->child, (now->childCount), sizeof(Node *),cmp);
     60     for(int i = 0; i < now->childCount; i++) {
     61         nSort(now->child[i]);
     62     }
     63 }
     64 
     65 void show(Node * now,int kong) {
     66     for(int i = 1; i <= kong; i++) {
     67         printf(" ");
     68     }
     69     puts(now->name);
     70     for(int i = 0; i < now->childCount; i++) {
     71         show(now->child[i],kong+strlen(now->name)+1);
     72     }
     73 }
     74 
     75 int main(int argc, char const *argv[])
     76 {
     77     int n;
     78     //freopen("input.txt","r",stdin);
     79     scanf("%d",&n);
     80     while( n != 0) {
     81         root.level = 0;
     82         root.childCount = 0;
     83         for(int i = 0; i < ChiM; i++) {
     84             root.child[i] = NULL;
     85         }
     86         for(int i = 0; i < n; i++) {
     87             Node *now = &root;
     88             scanf("%s",temp);
     89             int k = 0;
     90             for(int j = 0; j < strlen(temp); j++) {
     91                 if(temp[j] != '\') {
     92                     temp2[k++] = temp[j];
     93                 }
     94                 else {
     95                     temp2[k] = '';
     96                     k = 0;
     97                     now = add(now,temp2);
     98                 }
     99             }
    100             if(temp[strlen(temp)-1] != '\') {
    101                 temp2[k] = '';
    102                 now = add(now,temp2);
    103             }
    104 
    105         }
    106         nSort(&root);
    107         for(int i = 0; i < (root.childCount); i++) {
    108                 show(root.child[i],0);
    109         }
    110         printf("
    ");
    111         scanf("%d",&n);
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    “问答回复模块”Java开发文档官方改进版讲解【在线实习·吾研第二期】
    “付费邀请模块”产品原型图评审【在线实习·吾研第三期】
    “学长学姐认证模块”测试用例官方改进版讲解【在线实习·吾研第一期】
    “学长认证模块”Java代码2.0官方版要点讲解【在线实习·吾研第一期】
    “问答评论模块”UI作品评审【在线实习·吾研第二期】
    “认证模块”前端代码1.0评审【在线实习·吾研第一期】
    <<中国专利法详解>>学习笔记(一)
    JS 前端获得时间
    北漂的程序员
    Spring类注入异常
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5695197.html
Copyright © 2011-2022 走看看