forked from aditya81070/CodeForces-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path158-C-Shell.cpp
More file actions
110 lines (96 loc) · 2.08 KB
/
158-C-Shell.cpp
File metadata and controls
110 lines (96 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int GetNthIndex(string s, char t, int n)
{
int count = 0;
int lent = s.length();
for (int i = lent; i > 0; i--)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
int start_index(string s, char t, int n)
{
int count = 0;
int lent = s.length();
for (int i = 0; i <lent; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
int main(){
string command,pos = "/",last,temp,intial;
int next_slash,length,last_second,last_cond;
int no=0;
cin>>no;
while(no!=0){
cin>>intial;
if(intial=="cd"){
cin>>command;
}
if(intial=="pwd"){
cout<<pos<<endl;
}
else{
length = command.length();
if(start_index(command,'/',1)==0){
pos="/";
length = command.length();
command = command.substr(1,length-1);
}
char back =*command.rbegin();
if(back=='/'){
command = command.substr(0,length-1);
}
if(start_index(command,'/',1)==0){
length = command.length();
command = command.substr(1,length-1);
}
while(start_index(command,'/',1)!=-1){
next_slash = start_index(command,'/',1);
temp = command.substr(0,next_slash);
length = command.length();
if(temp==".."){
last_second=GetNthIndex(pos,'/',2);
pos = pos.substr(0,last_second+1);
}
else{
pos=pos+temp+'/';
}
command = command.substr(next_slash+1,length-next_slash);
}
if(command==".."){
last_second=GetNthIndex(pos,'/',2);
pos = pos.substr(0,last_second+1);
}
else{
if(command!=""){
pos=pos+command+'/';
}
}
if(start_index(pos,'/',1)!=0){
pos="/"+pos;
}
}
no--;
}
return 0;
}