fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int main()
  5. {
  6. int n,t;
  7. cin>>t;
  8. while(t--)
  9. {
  10. cin >> n;
  11. string cmd;
  12. char str[1000];
  13. char* arg;
  14. string s = "/";
  15. string path;
  16. while(n--)
  17. {
  18. cin >> cmd;
  19. if(cmd == "cd")
  20. {
  21. cin >> str;
  22. if(str[0] == '/')
  23. {
  24. s = "/";
  25. arg = str + 1;
  26. }
  27. else
  28. {
  29. arg = str;
  30. }
  31. int j;
  32. string cur = "";
  33. path = arg;
  34. path += "/";
  35. for(j = 0; j < path.length(); j++)
  36. {
  37. if(path[j] == '/')
  38. {
  39. if(cur == "..")
  40. {
  41. int k;
  42. for(k = s.length() - 2; k >= 0; k--)
  43. {
  44. if(s[k] == '/') break;
  45. }
  46. s.resize(k + 1);
  47. }
  48. else
  49. {
  50. s += cur;
  51. s += "/";
  52. }
  53. cur = "";
  54. }
  55. else
  56. {
  57. cur += path[j];
  58. }
  59. }
  60. }
  61. else
  62. {
  63. cout << s << endl;
  64. }
  65. }}
  66. return 0;
  67. }
Success #stdin #stdout 0s 2868KB
stdin
1
9
pwd
cd /home/csed
pwd
cd /lab/root/../dir
pwd
cd /home
pwd
cd lab
pwd
stdout
/
/home/csed/
/lab/dir/
/home/
/home/lab/