[string]Codeforces158C Cd and pwd commands

题目链接

题意很清楚 和linux的语句是一样的 

pwd输出路径 cd进入 ..回上一层目录

此题完全是string的应用

String的用法

 1 vector<string> s;
 2 int main()
 3 {
 4     int n;
 5     scanf("%d", &n);
 6     while(n--)
 7     {
 8         string cd;
 9         cin>>cd;
10         if(cd=="pwd")
11         {
12             printf("/");
13             for(int i=0;i<s.size();i++)
14                 cout<<s[i]<<"/";
15             puts("");
16         }
17         else
18         {
19             string path;
20             cin>>path;
21             if(path.end()[-1]!='/')
22                 path+='/';
23             while(path.find_first_of('/')!=string::npos)
24             {
25                 int pos=path.find_first_of('/');
26                 string a=path.substr(0, pos);
27                 path=path.substr(pos+1);
28                 if(a=="")
29                     s.clear();
30                 else if(a=="..")
31                     s.pop_back();
32                 else
33                     s.push_back(a);
34             }
35         }
36     }
37     return 0;
38 }
Codeforcs 158C
原文地址:https://www.cnblogs.com/Empress/p/4285581.html