⭐201604-3 Resolución de ruta

Lo que no entendí al principio de esta pregunta es cómo manejar las rutas relativas.
De hecho, ¡la ruta actual + la ruta relativa es una ruta absoluta!
Luego, si /…/ aparece al principio, simplemente elimínelo.
Si /.../ aparece en el medio, simplemente elimine el directorio anterior

#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;
}

Guess you like

Origin blog.csdn.net/susuate/article/details/120121386