-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016.4.3
125 lines (121 loc) · 2.09 KB
/
2016.4.3
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<sstream>
using namespace std;
int p;
string a;
string standard(string temp)
{
string ans;
int flag=0;
for(unsigned int i=0;i<temp.size();i++)
{
if(temp[i]=='/'&&flag==0)
{
ans=ans+temp[i];flag=1;
}
else if(temp[i]!='/')
{
ans=ans+temp[i];flag=0;
}
}
return ans;
}
void check(string str)
{
string ans;
if(str.size()==0)
{
cout<<a<<endl;
return ;
}
if(str[0]!='/')//从根开始
str=a+"/"+str;
/*
vector<string>q;
for(unsigned int i=0;i<str.size();i++)
{
if(str[i]=='/') str[i]=' ';
}
stringstream s(str);
string temp;
while(s>>temp){
if(temp==".")continue;
if(temp==".."){
if(!q.empty())q.pop_back();
continue;
}
q.push_back(temp);
}
if(q.empty()) cout<<"/";
else
{
for(unsigned int i=0;i<q.size();i++)
cout<<"/"<<q[i];
}
cout<<endl;*/
stack<string>q;
string temp;
if(str[str.size()-1]!='/') str+="/";
unsigned int i=1;
while(i<str.size())
{
unsigned int n=str.find('/',i);
if(n!=string::npos)
{
temp=str.substr(i,n-i);
if(temp.compare("..")==0)
{
if(q.size()!=0)
q.pop();
}
else if(temp.compare(".")==0)
{
}
else
{
q.push(temp);
}
i=n+1;
}
}
if(q.size()!=0){
for(unsigned int i=0;i<(q.size()-1);)
{
ans=q.top()+ans;
ans='/'+ans;
q.pop();
}
ans=q.top()+ans;
q.pop();
}
ans='/'+ans;
cout<<ans<<endl;
return ;
}
int main()
{
cin>>p>>a;
cin.get();
for(int i=0;i<p;i++)
{
string temp;
getline(cin,temp);
temp=standard(temp);
check(temp);
}
return 0;
}
/*
7
/d2/d3
/d2/d4/f1
/d4/f1
/d1/./f1
/d1///f1
/d1/
///
/d1/../../d2
*/