⭐201604-3 路径解析

则这道题一开始没有搞懂的就是相对路径要怎么处理
其实当前路径+相对路径就是绝对路径了!
然后如果/…/出现在一开始,就直接删掉。
如果/…/出现在中间,就把前面的上一个目录删掉即可

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
//路径解析
string str;//当前目录
string tempstr;//临时的需要正规化的路径
int main() {
	int n;
	scanf("%d", &n);
	getchar();
	getline(cin, str);
	for (int i = 0; i < n; i++)
	{
		getline(cin, tempstr);//getline可以输入空字符串		
		if (tempstr.length() == 0)//如果是空字符串,直接输出当前路径
		{			
			tempstr = str;
		}
		else
		{
			if (tempstr[0] == '/')
				;//说明是绝对路径,无需操作
			else
				tempstr = str + "/" + tempstr;//是相对路径,将相对路径变成绝对路径
			int fd=0;
			while ((fd = tempstr.find("/../")) != -1)
			{
				if (fd == 0)
					tempstr.replace(fd, 4, "/");
				else
				{
					int fd2 = tempstr.rfind("/", fd - 1);
					tempstr.erase(fd2, fd - fd2 + 3);
				}
			}
			
			fd = 0;
			while ((fd = tempstr.find("/./")) != -1)
			{
				tempstr.replace(fd, 3, "/");
			}			
			fd = 0;
			while ((fd = tempstr.find("//")) != -1)
			{
				tempstr.replace(fd, 2, "/");
			}			
			fd = 0;			
			if(tempstr.length() > 1&&tempstr[tempstr.length() - 1] == '/')//如果最后一个是/,那么删掉
			{				
				tempstr.erase(tempstr.length() - 1, 1);//删掉最后一个
			}
		}
		cout << tempstr << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值