路径解析_90

这个问题一开始用暴力解法很多地方过于繁琐,并且开始认为当前路径和之后输入的路径不能一起处理。。修修改改好多次。。思路堵塞。。后来参考了一个大佬的写法,真的历害思路清晰,可以把STL的函数用的很好。。有的函数虽然知道但没想起要用。。

90分代码如下(应该是输入上有些BUG):

#include<iostream>
#include<string>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int index;//指向数组下标
	string str_in,str_now;
	int p;
	cin>>p;
	cin>>str_now;
//	if(str_now.length()>=3) strfir = str_now.substr(str_now.length()-6,str_now.length()-4);
//	else strfir = "/";
//	Load no,input,yes;
//	string str_yes="";
	for(int i=1; i<=p; i++)
	{
		cin>>str_in;
		if(str_in[0]!='/')
		{
			str_in = str_now + "/" + str_in + "/";
		}
		while((index = str_in.find("//")) != -1)
		{
			int count = 2;
			while(str_in[index + count] == '/')
				count++;
			str_in.erase(index, count-1);
		}
//		index = 0;
//		end = str_in.length();
//		while(index < end)
//		{
//			if(str_in[index] == '.')
//			{
//				if(str_in[index+1] == '.')
//				{
//				}
//
//			}
//			else if(str_in[index] == '/')
//			{
//
//			}
//		}
		if(str_in.size() > 1 && str_in[str_in.size() - 1] == '/')
		{
			str_in.erase(str_in.size() - 1);
		}
		while((index = str_in.find("/./"))!=-1)
		{
			str_in.erase(index+1,2);
		}
		
		while((index = str_in.find("/../")) != -1)
		{
			if(index == 0)	str_in.erase(index, 3);
			else
			{
				int index_temp;
				index_temp = str_in.rfind("/", index - 1);
				str_in.erase(index_temp, index - index_temp + 3);
			}

			if(str_in.size() == 0)	str_in = "/";
		}
		cout<<str_in<<endl;
	}
	return 0;
}


posted @ 2017-11-19 22:24  予或  阅读(39)  评论(0编辑  收藏  举报