时间限制:C/C++ 1秒;其他语言 2秒
内存限制:C/C++ 32768KB;其他语言 65536KB
题目描述:
简化Unix风格的路径,需要考虑的包括“/../”,”//”,“/./”等情况
输入描述
Unix风格的路径
输出
简化后的Unix风格路径
样例输入
/a/./b/../../c/
样例输出
/c
解题思路:纯模拟
- “/.” 表示本级目录,可以忽略
- “/..” 表示返回上一级目录,即若上一级目录存在,连同“/..”一并删除,否则只删除“/..”
- 若去除冗余后路径为空,返回“/”
- 若包含多个连续“/”, 删除多余的“/”
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main()
{
string str;
list<string> path;
while(cin >> str)
{
path.clear();
for(int i = 0; i < str.length(); ++i)
{
if(str[i]=='/')
{
i++;
string tmp = "";
while(str[i]!='/' && i < str.length())
tmp+=str[i++];
i--;
if(tmp=="..")
{
if(!path.empty())
path.pop_back();
}
else if(tmp!="." && tmp!="")
path.push_back(tmp);
}
}
if(path.empty())
cout << "/";
while(!path.empty())
{
cout << "/" << path.front();
path.pop_front();
}
cout << endl;
}
return 0;
}
/*
/a/./b/../../c/
/home/
*/