题目描述:

给你一串路径,譬如:
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 ;
}

九度oj 题目1090:路径打印的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. 几款LINUX下的CHM查看器

    转帖地址:http://blog.csdn.net/aking21alinjuju/article/details/4436440 本文旨在介绍linux下的常见chm阅读软件及其安装,并针对一些问题 ...

  2. shell中使用ssh

    ssh服务是不能用非交互的方式传递密码,想不输入密码,直接ssh连接到服务器有两种方法,sshpass和expect sshpass # wget http://downloads.sourcefor ...

  3. Oracle Flashback Technology【闪回技术】

    -------------------------与其他数据库相比,Oracle的闪回让开发者多了一条选择的路. Flashback的目的 先看下Oracle官方文档中的解释: Oracle Flas ...

  4. 抽象常量class

    需要把经常用到的常量抽象到一个类里面管理 如:

  5. selenium-WebElement接口常用方法

    1.submit()方法用于提交表单. 例如:在收索框输入关键字之后的“回车”操作,就可以通过submit()方法模拟. 例如: from selenium import webdriverdrive ...

  6. 算法马拉松13 A-E解题报告

    A题意(取余最长路): 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱乐值为所经过的位置的权的总和. 有一天,她被下了恶毒的诅咒,这个诅咒的作用是将她的 ...

  7. python之dic {字典}(重要指数*****)

    1. 什么是字典 {'name': '汪峰', 'age': 18} '键':'值' 别的语言键值对数据 键: 必须是可哈希(不可变的数据类型),并且是唯一的 值: 任意 可以保存任意类型的数据 字典 ...

  8. 6.python

    最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字.01010101ascii 只能表示256种可能,太少,创办了万国码 unicode 16表示一个字符不行,32位表示一个字符. A ...

  9. WebStorm换主题(护眼)

    一.下载喜欢颜色的主题 http://www.phpstorm-themes.com/ 我用的豆沙绿护眼 <scheme name="Solarized Light My" ...

  10. PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)

    http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...