思路:
这道题要求把所有的路径都转化成不含"…“或”."的绝对路径输出,首先我们对于输入的字符串就要判断它是绝对路径还是相对路径。 对于相对路径,我们将根节点和这个相对路径做一个拼接,转化成绝对路径。
接下来就是对"…“和”.“以及”//"的处理。 对于… , 我们要找到每一个这样字符串的地方,然后删掉/…前面的一个路径和 … 即可。对于 . 我们直接删掉即可。然后找到//删掉即可。
代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> l;
int main(){
int n;
string str;
cin>>n>>str;
getchar();
for(int i=;i<=n;i++){
string s;
getline(cin,s);
if(s[]!='/') s=str+'/'+s;
if(s.size()==) s=str;
int pos;
while((pos=s.find("//"))!=-){
int count=;
while(s[pos+count]=='/') count++;
s.erase(pos,count-);
}
while((pos=s.find("/../"))!=-){
if(pos==){
s.erase(pos+,);
}
else{
int p=s.rfind('/',pos-);
s.erase(p,pos-p+);
}
}
while((pos=s.find("/./"))!=-){
s.erase(pos,);
}
if(s.size()>&&s[s.size()-]=='/')
s.erase(s.size()-);
l.push_back(s);
}
for(int i=;i<n;i++){
cout<<l[i]<<endl;
}
return ;
}