精简Linux文件路径
精简Linux的文件路径:
- ..回退的功能
- .留在当前文件夹
- //仅仅保留一个/
- abc/..要返回.
- 报错
- 删除最后一个/
#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <stack>
using namespace std;
int selectK(int num[], int k, int l, int r) { assert(k <= (r - l + 1) && k >= 1);
int mid = (l + r) / 2, i = l, j = r;
while (i <= j) {
while (num[i] < num[mid]) {
++i;
}
while (num[j] > num[mid]) {
--j;
}
if (i <= j) {
swap(num[i],num[j]);
++i,--j;
}
}
if (k == i - l)
return num[i - 1];
else if (k < i - l)
return selectK(num, k, l, i -1);
else if (k == j + 1 - l)
return num[j + 1];
else
return selectK(num, k - (j - l + 2), j + 2, r);
}
void pathcompress(char* str) {
int i = 0, j = 0; char prev = '\0';
stack<int> offset;
offset.push(0);
while(str[i]) {
if (prev == '/') {
if (str[i] == '.' && (str[i + 1] == '/' || str[i + 1] == '\0')) {
prev = str[i + 1], i += 1;
}
else if (str[i] == '.' && str[i + 1] == '.' && (str[i + 2] == '/' || str[i + 2] == '\0')) {
i += 2;
if (offset.empty()) {
cout << "error" << endl;
return;
}
j = offset.top();
offset.pop();
if (offset.empty() && str[0] == '/') {
cout << "error" << endl;
return;
}
}
else if (str[i] == '/') {
prev = str[i++];
}
else {
offset.push(j);
prev = str[i];
str[j++] = str[i++];
}
}
else {
prev = str[i];
str[j++] = str[i++];
}
}
if (j >=3 && str[j - 1] == '/' && str[j-2] == '/')
str[j-2] = '\0';
else if (j >= 2 && str[j - 1] == '/')
str[j-1] = '\0';
else
str[j] = '\0';
if (str[0] == '\0'){
str[0] = '.';
str[1] = '\0';
} } int main()
{
int num[] = {3,2,1,4,5};
int res1 = selectK(num, 1, 0, 4);
int res2 = selectK(num, 2, 0, 4);
int res3 = selectK(num, 3, 0, 4);
int res4 = selectK(num, 4, 0, 4);
int res5 = selectK(num, 5, 0, 4);
// int res6 = selectK(num, 6, 0, 4); //char str[] = "////";
char str[] = "/.abc/xxx./abc/bacd/.././bcd/fsgs/../../../x/y/z/../../../..";
pathcompress(str);
printf("%s\n",str); return 0;
}
BUT this isn't right, For example: char str[] = "../../../etc/xyz/../abc"; You couldn't print error here. The right solution is:
#include <iostream>
#include <map>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <stack>
using namespace std;
int selectK(int num[], int k, int l, int r) { assert(k <= (r - l + 1) && k >= 1);
int mid = (l + r) / 2, i = l, j = r;
while (i <= j) {
while (num[i] < num[mid]) {
++i;
}
while (num[j] > num[mid]) {
--j;
}
if (i <= j) {
swap(num[i],num[j]);
++i,--j;
}
}
if (k == i - l)
return num[i - 1];
else if (k < i - l)
return selectK(num, k, l, i -1);
else if (k == j + 1 - l)
return num[j + 1];
else
return selectK(num, k - (j - l + 2), j + 2, r);
} void pathcompress2(char* str) {
stack<int> path;
int i = 0, j = 0;
bool isRoot = (str[0] == '/');
char prev = '\0';
int len = strlen(str); if (!(len >= 3 && str[0] == '.' && str[1] == '.' && str[2] == '/'))
path.push(0);
while(str[i]) {
if (prev == '/') {
if (str[i] == '.' && (str[i+1] == '/' || str[i+1] == '\0')) {
prev = '/'; if (str[i+1] == '\0') {
str[j] = '\0';
break;
}
i+=2;
}
else if (str[i] == '.' && str[i+1] == '.' && (str[i+2] == '/' || str[i+2] == '\0')) {
if (path.empty()) {
str[j++] = str[i];
str[j++] = str[i+1];
str[j++] = str[i+2];
prev = '/'; if (str[i+2] == '\0') {
str[j] = '\0';
break;
}
i+=3;
}
else {
j = path.top();
path.pop(); if (path.empty() && isRoot) { // The case : cd /..
printf("Error\n");
return;
}
if (str[i+2] == '\0') {
str[j] = '\0';
break;
}
prev = '/';
i += 3;
}
}
else if (str[i] == '/')
prev = str[i++];
else {
prev = str[i];
path.push(j);
str[j++] = str[i++];
}
}
else {
prev = str[i];
str[j++] = str[i++];
}
} if (j >= 2 && str[j - 1] == '/')
str[j-1] = '\0';
else
str[j] = '\0';
if (str[0] == '\0'){
str[0] = '.';
str[1] = '\0';
} } int main()
{
int num[] = {3,2,1,4,5};
int res1 = selectK(num, 1, 0, 4);
int res2 = selectK(num, 2, 0, 4);
int res3 = selectK(num, 3, 0, 4);
int res4 = selectK(num, 4, 0, 4);
int res5 = selectK(num, 5, 0, 4);
// int res6 = selectK(num, 6, 0, 4); char str[] = "/.abc/xxx./abc/bacd/.././bcd/fsgs/../../../x/y/z/../../../../.././././xda";
//char str[] = "asdf/.abc/xxx./abc/bacd/.././bcd/fsgs/../../../x/y/z/../../../../../../.././../.././././";
//char str[] = "/xyz/./bcd/fsgs/../../../x/y/z/../../../..";
//char str[] = "../../../etc/xyz/../abc/////////////////////////////.asdf/../../../../"; pathcompress2(str);
printf("%s\n",str); return 0;
}
The concise version is :
void pathcompress2(char* str) {
stack<int> path;
int i = 0, j = 0, len = strlen(str);
bool isRoot = (str[0] == '/');
char prev = '\0';
if (!(len >= 3 && str[0] == '.' && str[1] == '.' && (str[2] == '/' || str[2] == '\0'))
path.push(0);
while(str[i]) {
if (prev == '/') {
if (str[i] == '.' && (str[i+1] == '/' || str[i+1] == '\0')) {
prev = '/';
if (str[i+1] == '\0') {
str[j] = '\0';
break;
}
i+=2;
}
else if (str[i] == '.' && str[i+1] == '.' && (str[i+2] == '/' || str[i+2] == '\0')) {
if (path.empty()) {
str[j++] = str[i],str[j++] = str[i+1],str[j++] = str[i+2],prev = '/';
}
else {
j = path.top();
path.pop();
if (path.empty() && isRoot) { // The case : cd /..
printf("Error\n");
return;
}
}
if (str[i+2] == '\0') {
str[j] = '\0';
break;
}
prev = '/',i += 3;
}
else if (str[i] == '/')
prev = str[i++];
else {
prev = str[i],path.push(j),str[j++] = str[i++];
}
}
else {
prev = str[i],str[j++] = str[i++];
}
}
if (j >= 2 && str[j - 1] == '/')
str[j-1] = '\0';
else
str[j] = '\0';
if (str[0] == '\0'){
str[0] = '.',str[1] = '\0';
}
}
精简Linux文件路径的更多相关文章
- Linux 文件路径包含特殊字符的处理方式
文件路径包含特殊字符的处理方式 1)只用转义符 \ 2)使用双引号 mv /home/".Sent Items"/ /home/".&XfJT0ZABkK5O9g ...
- 截取linux文件存储路径方法
1.截取linux文件存储路径方法 package com.tydic.eshop.action.freemarker; public class dddd { public static void ...
- 【linux下查看文件路径--jdk】
1.which java 首先输入命令行,查看结果: [root@localhost ~]# which java /usr/bin/java PS:which Java是无法定位到Java的安装路径 ...
- Linux下如何查看tomcat是否安装、启动、文件路径、进程ID
Linux下如何查看tomcat是否安装.启动.文件路径.进程ID 在Linux系统下,Tomcat使用命令的操作! 检测是否有安装了Tomcat: rpm -qa|grep tomcat 查看Tom ...
- linux sed命令查询结果前后批量追加内容(html文件批量修改css,js等文件路径)
1.需求:linux使用shell命令查询结果前后批量追加内容 例如:我需要在当前目录下所有的css文件路径前追加域名 我想的是用sed替换去实现,鲍哥的思路是用for循环 1.1方法1:鲍哥的for ...
- 关于File.separator 文件路径:window与linux下路径问题(“No such file or diretory ”异常解决方案)
最近有个在页面上传Excel文件至服务器指定目录并进行数据校验.最后入库及进行进一步处理的应用情境,我写好代码在模拟环境下测试,完全没问题:但客户试用的时候,却老是报告“No such file or ...
- linux whereis-查找二进制程序、代码等相关文件路径
推荐:更多Linux 文件查找和比较 命令关注:linux命令大全 whereis命令用来定位指令的二进制程序.源代码文件和man手册页等相关文件的路径. whereis命令只能用于程序名的搜索,而且 ...
- Linux库文件路径的添加
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时被使用,其搜索路径是在系统中进行设置的.一般 Linux 系统把 /lib 和 /usr/lib 两个目录作为默认的库搜索路径,所以使用 ...
- linux 头文件路径
linux 头文件路径 /usr/include
随机推荐
- hdoj--2015--偶数求和(水题)
偶数求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 南海区行政审批管理系统接口规范v0.3(规划)4.2.【queryExpireList】当天到期业务查询
加密前:{"time":"1510053168455","username":"GH_DATA_EXCHANGE",&q ...
- k8s Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry
环境介绍: 说明 节点 ip 系统 Gitlab Server git.ds.com 10.0.1.179 CentOS 7.5.1804 Gitlab Runner 10.0.1.178 Cen ...
- flex和layout移动端布局
1.九宫格 样式为: ul{ display: flex; flex-wrap: wrap;//超出换行 } li{ width: 33%; height: 60px; display: flex; ...
- web 端即时通讯
1. 前言 Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Serve ...
- python - 中文打印报错SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no encoding declared。
python中默认的编码格式是ASCII格式, 所以在没修改编码格式时无法正确打印汉字. 解决办法: 在以后的每一个需要显示汉字的python文件中, 可以采用如下方法在 #!/usr/bin/pyt ...
- 6.Renderer Window
渲染是实时的,所见即所得.同时还可以输出一些统计信息. Pixel Snoop:获取颜色值,同时把该值复制到剪贴板,主要用户是获取颜色值 Wireframe:开启后可以查看3D节点图形骨架 Stati ...
- 【技术累积】【点】【java】【4】日志级别
闲聊 水文也是文,写总比不写好. 日志级别 虽然对其他语言的日志系统也不甚了解,但还是感觉Java的日志有些麻烦,当然也可以说是发展已久,多有变化,多有完善吧. 从日志级别来说,有从高到低的八个级别: ...
- [Intermediate Algorithm] - Sum All Odd Fibonacci Numbers
题目 给一个正整数num,返回小于或等于num的斐波纳契奇数之和. 斐波纳契数列中的前几个数字是 1.1.2.3.5 和 8,随后的每一个数字都是前两个数字之和. 例如,sumFibs(4)应该返回 ...
- C#获取硬盘序列号
//创建ManagementObjectSearcher对象 ManagementObjectSearcher searcher = new ManagementObjectSearcher(&quo ...