题目描述:

给你一串路径,譬如:
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 这道题我用了比较复杂的数据结构,但题目的输出说的不是很明确,代码如下
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
#define LenM 52
#define ChiM 12
using namespace std; struct Node
{
char name[LenM];
int level;
int childCount;
Node *child[ChiM];
}; Node root;
char temp[LenM];
char temp2[LenM]; int cmp(const void *a, const void *b) {
Node *at = *(Node**)a;
Node *bt = *(Node**)b;
return strcmp(at->name,bt->name);
}
Node *add(Node *now, char toAdd[]) {
bool isFind = false;
int findNum = -;
for(int i = ; i < now->childCount; i++) {
if(strcmp(toAdd, (now->child[i])->name) == ) {
isFind = true;
findNum = i;
break;
}
}
if(!isFind) {
Node *childNode = new Node;
childNode->level = now->level + ;
strcpy(childNode->name, toAdd);
childNode->name[strlen(toAdd)] = '\0';
childNode->childCount = ;
for(int i = ; i < ChiM; i++) {
childNode->child[i] = NULL;
}
now->child[now->childCount] = childNode;
(now->childCount)++;
return childNode;
}
else {
return now->child[findNum];
}
} void nSort(Node *now) {
qsort(now->child, (now->childCount), sizeof(Node *),cmp);
for(int i = ; i < now->childCount; i++) {
nSort(now->child[i]);
}
} void show(Node * now,int kong) {
for(int i = ; i <= kong; i++) {
printf(" ");
}
puts(now->name);
for(int i = ; i < now->childCount; i++) {
show(now->child[i],kong+strlen(now->name)+);
}
} int main(int argc, char const *argv[])
{
int n;
//freopen("input.txt","r",stdin);
scanf("%d",&n);
while( n != ) {
root.level = ;
root.childCount = ;
for(int i = ; i < ChiM; i++) {
root.child[i] = NULL;
}
for(int i = ; i < n; i++) {
Node *now = &root;
scanf("%s",temp);
int k = ;
for(int j = ; j < strlen(temp); j++) {
if(temp[j] != '\\') {
temp2[k++] = temp[j];
}
else {
temp2[k] = '\0';
k = ;
now = add(now,temp2);
}
}
if(temp[strlen(temp)-] != '\\') {
temp2[k] = '\0';
now = add(now,temp2);
} }
nSort(&root);
for(int i = ; i < (root.childCount); i++) {
show(root.child[i],);
}
printf("\n");
scanf("%d",&n);
}
return ;
}

最新文章

  1. socket编程--socket模块介绍
  2. Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析
  3. 20145337实验五Java网络编程及安全
  4. 数据结构&amp;算法-单链表
  5. SqlDevlepor注册表监听器设置
  6. android ping网络是否成功
  7. ZooKeeper(3.4.5) - 原生 API 的简单示例
  8. mysql net连接读取结果为乱码 Incorrect string value
  9. Linux 硬连接和软连接的原理 (in使用)
  10. 【Heritrix基础教程之2】Heritrix基本内容介绍
  11. KVO 的使用和举例
  12. Android万能适配器base-adapter-helper的源代码分析
  13. [转]理解C# 4 dynamic(1) - var, object, dynamic的区别以及dynamic的使用
  14. SmtpDlg 调用SMTP
  15. C#中关于WebBrowser的一些细节设置
  16. 使用axios向后端传递数据,后端接收不到?
  17. poj 2886 线段树+反素数
  18. Jeecg-Boot前后端分离版
  19. yii2 使用指定数据库执行createCommand
  20. django中使用memcache的一些注意事项

热门文章

  1. Emacs Org-mode中英文字体设置
  2. tar.gz
  3. 解决更新到os x10.11后openssl头文件无法找到的问题
  4. Luogu P5349 幂
  5. java并发编程:Executor、Executors、ExecutorService
  6. Vue项目经验
  7. python中return和yield
  8. Bootstrap 网格系统(Grid System)实例4
  9. Leetcode 71 简化路径simplify-path(栈)
  10. LeetCode 朋友圈