C. Cd and pwd commands

题目链接:http://codeforces.com/problemset/problem/158/C

题意:

模拟对文件路径的操作。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdlib.h>
 4 #include <string>
 5 #include <string.h>
 6 #include <set>
 7 #include <queue>
 8 #include <stdbool.h>
 9  
10 #define LL long long
11 using namespace std;
12 const int maxn = 1e6 + 10;
13  
14  
15 int main(){
16     int n;
17     scanf("%d",&n);
18     string s,p = "/",temp;
19     for (int i=0;i<n;i++){
20         cin >> s;
21         if (s == "cd"){
22             cin >> s;
23             s += "/";
24             for (int j=0;j<s.length();j++){
25                 temp += s[j];
26                 if (s[j] == '/'){
27                     if (temp == "/"){
28                          p = temp;
29                     }
30                     else if (temp == "../"){
31                         int k;
32                         for (k=p.length()-1;p[k-1]!='/';k--)
33                             ;
34                         p.resize(k);
35                     }
36                     else {
37                         p += temp;
38                     }
39                     temp = "";
40                 }
41             }
42         }
43         else if (s == "pwd"){
44             cout << p << endl;
45         }
46     }
47 }

猜你喜欢

转载自www.cnblogs.com/-Ackerman/p/11371030.html