转:Linux文件路径

精简Linux的文件路径:

  1. ..回退的功能
  2. .留在当前目录
  3. //只保留一个/
  4. abc/..要返回.
  5. 报错
  6. 删除最后一个/
主要思路: 用栈记录路径的起始位置,讨论/后的不同情况即可:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <map>  
  3. #include <algorithm>  
  4. #include <limits.h>  
  5. #include <assert.h>  
  6. #include <stack>  
  7. using namespace std;  
  8. int selectK(int num[], int k, int l, int r) {  
  9.     
  10.   assert(k <= (r - l + 1) && k >= 1);  
  11.   int mid = (l + r) / 2, i = l, j = r;  
  12.   while (i <= j) {  
  13.     while (num[i] < num[mid]) {  
  14.       ++i;  
  15.     }  
  16.     while (num[j] > num[mid]) {  
  17.       --j;  
  18.     }  
  19.     if (i <= j) {  
  20.       swap(num[i],num[j]);  
  21.       ++i,--j;  
  22.     }  
  23.   }  
  24.   if (k == i - l)  
  25.     return num[i - 1];  
  26.   else if (k < i - l)  
  27.     return selectK(num, k, l, i -1);  
  28.   else if (k == j + 1 - l)  
  29.     return num[j + 1];  
  30.   else  
  31.     return selectK(num, k - (j - l + 2), j + 2, r);  
  32. }  
  33. void pathcompress(char* str) {  
  34.   int i = 0, j = 0;  
  35.   
  36.   char prev = '\0';  
  37.   stack<int> offset;  
  38.   offset.push(0);  
  39.   while(str[i]) {  
  40.     if (prev == '/') {  
  41.         if (str[i] == '.' && (str[i + 1] == '/' || str[i + 1] == '\0')) {  
  42.           prev = str[i + 1], i += 1;  
  43.         }  
  44.         else if (str[i] == '.' && str[i + 1] == '.' && (str[i + 2] == '/' || str[i + 2] == '\0')) {  
  45.           i += 2;  
  46.           if (offset.empty()) {  
  47.             cout << "error" << endl;  
  48.             return;              
  49.           }  
  50.           j = offset.top();  
  51.           offset.pop();  
  52.           if (offset.empty() && str[0] == '/') {  
  53.             cout << "error" << endl;  
  54.             return;  
  55.           }  
  56.         }  
  57.         else if (str[i] == '/') {  
  58.           prev = str[i++];  
  59.         }  
  60.         else {  
  61.           offset.push(j);  
  62.           prev = str[i];  
  63.           str[j++] = str[i++];          
  64.         }  
  65.     }  
  66.     else {  
  67.       prev = str[i];  
  68.       str[j++] = str[i++];  
  69.     }  
  70.   }  
  71.   if (j >=3 && str[j - 1] == '/' && str[j-2] == '/')  
  72.     str[j-2] = '\0';  
  73.   else if (j >= 2 && str[j - 1] == '/')  
  74.     str[j-1] = '\0';  
  75.   else  
  76.     str[j] = '\0';  
  77.   if (str[0] == '\0'){  
  78.     str[0] = '.';  
  79.     str[1] = '\0';  
  80.   }  
  81.       
  82. }  
  83.   
  84.   
  85. int main()  
  86. {  
  87.   int num[] = {3,2,1,4,5};  
  88.   int res1 = selectK(num, 1, 0, 4);  
  89.   int res2 = selectK(num, 2, 0, 4);  
  90.   int res3 = selectK(num, 3, 0, 4);  
  91.   int res4 = selectK(num, 4, 0, 4);  
  92.   int res5 = selectK(num, 5, 0, 4);  
  93.  // int res6 = selectK(num, 6, 0, 4);  
  94.   
  95.   //char str[] = "";  
  96.   char str[] = "/.abc/xxx./abc/bacd/.././bcd/fsgs/../../../x/y/z/../../../..";  
  97.   pathcompress(str);  
  98.   printf("%s\n",str);  
  99.   
  100.   return 0;  
  101. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值