集训final D STL中string的应用
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
vector<string>s;
while(!s.empty())
{
s.pop_back();
}
while(n--)
{
string cmd;
cin>>cmd;
if(cmd=="cd")
{
string path;
cin>>path;
int pos=path.find("/");
if(pos==0)
{
while(!s.empty())
{
s.pop_back();
}
path.erase(0,1);
}
pos=path.find("/");
while(pos!=string::npos)
{
if(path.substr(0,pos)=="..")
{
if(!s.empty())
{
s.pop_back();
}
}else
{
s.push_back(path.substr(0,pos));
}
path.erase(0,pos+1);
pos=path.find("/");
}
if(path.length()!=0)
{
if(path=="..")
{
if(!s.empty())
{
s.pop_back();
}
}else
{
s.push_back(path);
}
}
/*
string path;
cin>>path;
int pos=path.find("/");
if(pos!=0)
{
int pos_next=path.find("/",pos+1);
string pname=substr(pos,pos_next-pos-1);
if(pname=="..")
{
if(!s.empty())
{
s.pop();
}
}
while(s.fornt()!=pname)
{
}
}
while(pos!=string::npos)
{
int pos_next=path.find("/",pos+1);
if(pos_next!=string::npos)
{
string pname=substr(pos,pos_next-pos-1);
if(pname=="..")
{
if(!s.empty())
{
s.pop_back();
}
}
}
}*/
}else if(cmd=="pwd")
{
int i;
cout<<"/";
for(i=0;i<=int(s.size())-1;i++)
{
cout<<s[i]<<"/";
}
cout<<endl;
}
}
}
return 0;
}
本博客(http://www.cnblogs.com/cj695/)未标明转载的内容均为本站原创,非商业用途转载时请署名(77695)并注明来源(http://www.cnblogs.com/cj695/)。商业用途请联系作者(77695) QQ:646710030。作者(77695)保留本博客所有内容的一切权利。
独立博客:http://nfeng.cc/
独立博客:http://nfeng.cc/

浙公网安备 33010602011771号