鱼C论坛

 找回密码
 立即注册
查看: 2232|回复: 8

ccf 路径解析原题,要C语言的

[复制链接]
发表于 2016-9-10 16:45:03 | 显示全部楼层 |阅读模式
50鱼币
问题描述
  在操作系统中,数据通常以文件的形式存储在文件系统中。文件系统一般采用层次化的组织形式,由目录(或者文件夹)和文件构成,形成一棵树的形状。文件有内容,用于存储数据。目录是容器,可包含文件或其他目录。同一个目录下的所有文件和目录的名字各不相同,不同目录下可以有名字相同的文件或目录。
  为了指定文件系统中的某个文件,需要用路径来定位。在类 Unix 系统(Linux、Max OS X、FreeBSD等)中,路径由若干部分构成,每个部分是一个目录或者文件的名字,相邻两个部分之间用 / 符号分隔。
  有一个特殊的目录被称为根目录,是整个文件系统形成的这棵树的根节点,用一个单独的 / 符号表示。在操作系统中,有当前目录的概念,表示用户目前正在工作的目录。根据出发点可以把路径分为两类:
  Ÿ 绝对路径:以 / 符号开头,表示从根目录开始构建的路径。
  Ÿ 相对路径:不以 / 符号开头,表示从当前目录开始构建的路径。

  例如,有一个文件系统的结构如下图所示。在这个文件系统中,有根目录 / 和其他普通目录 d1、d2、d3、d4,以及文件 f1、f2、f3、f1、f4。其中,两个 f1 是同名文件,但在不同的目录下。

  对于 d4 目录下的 f1 文件,可以用绝对路径 /d2/d4/f1 来指定。如果当前目录是 /d2/d3,这个文件也可以用相对路径 ../d4/f1 来指定,这里 .. 表示上一级目录(注意,根目录的上一级目录是它本身)。还有 . 表示本目录,例如 /d1/./f1 指定的就是 /d1/f1。注意,如果有多个连续的 / 出现,其效果等同于一个 /,例如 /d1///f1 指定的也是 /d1/f1。
  本题会给出一些路径,要求对于每个路径,给出正规化以后的形式。一个路径经过正规化操作后,其指定的文件不变,但是会变成一个不包含 . 和 .. 的绝对路径,且不包含连续多个 / 符号。如果一个路径以 / 结尾,那么它代表的一定是一个目录,正规化操作要去掉结尾的 /。若这个路径代表根目录,则正规化操作的结果是 /。若路径为空字符串,则正规化操作的结果是当前目录。
输入格式
  第一行包含一个整数 P,表示需要进行正规化操作的路径个数。
  第二行包含一个字符串,表示当前目录。
  以下 P 行,每行包含一个字符串,表示需要进行正规化操作的路径。
输出格式
  共 P 行,每行一个字符串,表示经过正规化操作后的路径,顺序与输入对应。
样例输入
7
/d2/d3
/d2/d4/f1
../d4/f1
/d1/./f1
/d1///f1
/d1/
///
/d1/../../d2
样例输出
/d2/d4/f1
/d2/d4/f1
/d1/f1
/d1/f1
/d1
/
/d2
评测用例规模与约定
  1 ≤ P ≤ 10。
  文件和目录的名字只包含大小写字母、数字和小数点 .、减号 - 以及下划线 _。
  不会有文件或目录的名字是 . 或 .. ,它们具有题目描述中给出的特殊含义。
  输入的所有路径每个长度不超过 1000 个字符。
  输入的当前目录保证是一个经过正规化操作后的路径。
  对于前 30% 的测试用例,需要正规化的路径的组成部分不包含 . 和 .. 。
  对于前 60% 的测试用例,需要正规化的路径都是绝对路径。

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-10 16:45:04 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define PATHLEN 1024


  5. /***    log    ***/
  6. //#define DEBUG
  7. #ifdef DEBUG
  8. #define LOG(format, ...) \
  9.         printf("<"__FILE__">(%05d): "format"", __LINE__, ##__VA_ARGS__)
  10. #else
  11. #define LOG(format, ...)
  12. #endif
  13. /***    log end    ***/


  14. /***    list    ***/
  15. typedef struct _SNode {
  16.         struct _SNode * prev;
  17.         struct _SNode * next;
  18.         char m_acDir[PATHLEN];
  19. }SNode;

  20. typedef struct _SList {
  21.         struct _SNode * head;
  22.         struct _SNode * tail;
  23. }SList;

  24. SNode * list_node_init(const char * pcDir)
  25. {
  26.         SNode * pNode = (SNode*)malloc(sizeof(SNode));
  27.         pNode->prev = NULL;
  28.         pNode->next = NULL;
  29.         if(NULL == pcDir)
  30.         {
  31.                 memset(pNode->m_acDir, 0, sizeof(pNode->m_acDir));
  32.         }
  33.         else
  34.         {
  35.                 strcpy(pNode->m_acDir, pcDir);
  36.         }
  37.         return pNode;
  38. }

  39. SList * list_init()
  40. {
  41.         SList * pList = (SList*)malloc(sizeof(SList));
  42.         SNode * pHead = list_node_init(NULL);
  43.         pList->head = pHead;
  44.         pList->tail = pHead;
  45.         return pList;
  46. }

  47. void list_insert_one_node(SNode * pNewNode, SNode * pPreNode, SNode * pCurNode)
  48. {
  49.         pNewNode->prev = pPreNode;
  50.         pNewNode->next = pCurNode;

  51.         pPreNode->next = pNewNode;

  52.         if(pCurNode != NULL)
  53.         {
  54.                 pCurNode->prev = pNewNode;
  55.         }
  56. }

  57. void list_delete_one_node(SNode * pNode)
  58. {
  59.         if(NULL == pNode) {
  60.                 return ;
  61.         }

  62.         SNode * pPreNode = pNode->prev;
  63.         SNode * pNextNode = pNode->next;

  64.         pPreNode->next = pNextNode;

  65.         if(pNextNode != NULL) {
  66.                 pNextNode->prev = pPreNode;
  67.         }

  68.         free(pNode);
  69. }

  70. void list_insert_node_tail(SList * pList, SNode * pNode)
  71. {
  72.         list_insert_one_node(pNode, pList->tail, NULL);
  73.         pList->tail = pNode;
  74. }

  75. void list_insert_tail(SList * pList, const char * pcData)
  76. {
  77.         SNode * pNode = list_node_init(pcData);
  78.         list_insert_node_tail(pList, pNode);
  79. }

  80. SList * list_join(SList * first, SList * second)
  81. {
  82.         SList * pNewList = list_init();
  83.         pNewList->tail->next = first->head->next;
  84.         first->head->next->prev = pNewList->tail;

  85.         first->tail->next = second->head->next;
  86.         second->head->next->prev = first->tail;

  87.         first->head->next = NULL;
  88.         first->tail = first->head;
  89.         second->head->next = NULL;
  90.         second->tail = second->head;
  91.         return pNewList;
  92. }

  93. void list_destroy(SList * pList)
  94. {
  95.         if(NULL == pList) {
  96.                 return ;
  97.         }

  98.         SNode * pCur = pList->head->next;
  99.         while (pCur != NULL)
  100.         {
  101.                 SNode * pNext = pCur->next;
  102.                 free(pCur);
  103.                 pCur = pNext;
  104.         }
  105.         free(pList);
  106. }

  107. void list_print(SList * pList)
  108. {
  109. #ifdef DEBUG
  110.         SNode * pCur = pList->head->next;
  111.         while (pCur != NULL)
  112.         {
  113.                 printf("%s ", pCur->m_acDir);
  114.                 pCur = pCur->next;
  115.         }
  116.         printf("\n");
  117. #endif
  118. }
  119. /***    list end    ***/


  120. SList * pstChangePathToList(const char * pcPath)
  121. {
  122.         SList * pList = list_init();

  123.         char acPath[PATHLEN] = {0};
  124.         strcpy(acPath, pcPath);
  125.         char * pStart = acPath;

  126.         if(pStart[0] == '/') //absolute path
  127.         {
  128.                 list_insert_tail(pList, "/");
  129.                 pStart++;
  130.         }

  131.         while (*pStart != '\0')
  132.         {
  133.                 char * pEnd = strchr(pStart, '/');
  134.                 if(NULL == pEnd) //last one dir
  135.                 {
  136.                         list_insert_tail(pList, pStart);
  137.                         break;
  138.                 }
  139.                 else
  140.                 {
  141.                         if( pEnd == pStart ) //serial '/'
  142.                         {
  143.                                 pStart++;
  144.                         }
  145.                         else
  146.                         {
  147.                                 *pEnd = '\0';
  148.                                 list_insert_tail(pList, pStart);
  149.                                 pStart = pEnd + 1;
  150.                         }
  151.                 }
  152.         }

  153.         list_print(pList);
  154.         return pList;
  155. }

  156. void vTrimAbsolutePath(SList * pList, char * pcPath)
  157. {
  158.         SNode * pCur = pList->head->next->next;
  159.         if(NULL == pCur)
  160.         {
  161.                 strcpy(pcPath, "/");
  162.                 return ;
  163.         }

  164.         //delete '.' and '..'
  165.         while (pCur != NULL)
  166.         {
  167.                 if( strcmp(".", pCur->m_acDir) == 0 )
  168.                 {
  169.                         SNode * pNext = pCur->next;
  170.                         list_delete_one_node(pCur);
  171.                         pCur = pNext;
  172.                 }
  173.                 else if( strcmp("..", pCur->m_acDir) == 0 )
  174.                 {
  175.                         SNode * pNext = pCur->next;
  176.                         list_delete_one_node(pCur);
  177.                         pCur = pNext;

  178.                         if(pCur != NULL && pCur->prev != pList->head->next) { //if prev is root '/'
  179.                                 list_delete_one_node(pCur->prev);
  180.                         }
  181.                 }
  182.                 else
  183.                 {
  184.                         pCur = pCur->next;
  185.                 }
  186.         }

  187.         pCur = pList->head->next->next;
  188.         if(NULL == pCur)
  189.         {
  190.                 strcpy(pcPath, "/");
  191.                 return ;
  192.         }

  193.         while (pCur != NULL)
  194.         {
  195.                 strcat(pcPath, "/");
  196.                 strcat(pcPath, pCur->m_acDir);
  197.                 pCur = pCur->next;
  198.         }
  199. }

  200. void vTrimRelativePath(SList * pCurList, SList * pSrcList, char * pcPath)
  201. {
  202.         SList * pList = list_join(pCurList, pSrcList);
  203.         vTrimAbsolutePath(pList, pcPath);
  204.         list_destroy(pList);
  205. }

  206. void vGetAbsolutePath(const char * pcCurPath, const char * pcSrc, char * pcDst)
  207. {
  208.         if(NULL == pcCurPath || NULL == pcSrc || NULL == pcDst) {
  209.                 return ;
  210.         }
  211.         LOG("pcCurPath: %s\n", pcCurPath);
  212.         LOG("pcSrc: %s\n", pcSrc);

  213.         SList * pCurList = pstChangePathToList(pcCurPath);
  214.         SList * pSrcList = pstChangePathToList(pcSrc);

  215.         if(pcSrc[0] == '/') //absolute path
  216.         {
  217.                 vTrimAbsolutePath(pSrcList, pcDst);
  218.         }
  219.         else //relative path
  220.         {
  221.                 vTrimRelativePath(pCurList, pSrcList, pcDst);
  222.         }

  223.         list_destroy(pCurList);
  224.         list_destroy(pSrcList);
  225. }

  226. int main()
  227. {
  228.         int n = 0;
  229.         char acCurPath[PATHLEN] = {0};

  230.         scanf("%d", &n);
  231.         scanf("%s", acCurPath);

  232.         for (int i = 0; i < n; i++)
  233.         {
  234.                 char acSrcPath[PATHLEN] = {0};
  235.                 char acDstPath[PATHLEN] = {0};

  236.                 scanf("%s", acSrcPath);
  237.                 vGetAbsolutePath(acCurPath, acSrcPath, acDstPath);
  238.                 LOG("acDstPath: %s\n\n", acDstPath);
  239.                 printf("%s\n", acDstPath);
  240.         }
  241.         return 0;
  242. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-10 21:58:47 | 显示全部楼层
写了一个晚上,希望你喜欢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-10 22:21:12 | 显示全部楼层
作为一个渣,看不懂题目的我只对楼上充满无限敬意!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-11 15:40:24 | 显示全部楼层
看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-9-12 23:36:51 | 显示全部楼层

谢谢啊!这么晚才回复失礼了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-9-12 23:41:00 | 显示全部楼层

谢谢啊!这么晚才回复失礼了!!!
虽然在ccf上显示70, 我想还是不错了,thanks
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-13 19:55:14 | 显示全部楼层
小布点^^ 发表于 2016-9-12 23:41
谢谢啊!这么晚才回复失礼了!!!
虽然在ccf上显示70, 我想还是不错了,thanks

ccf是什么东西?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-23 13:36:32 | 显示全部楼层
被虐了发个表情支持下!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 19:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表