您的位置:首页 > 其它

ccf 201604-3 路径解析

2017-08-22 14:48 316 查看
用栈表示路径,相对路径相当于开始的时候当前目录已经在栈中

#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> dir;
void f(string &s, vector<string> &d) {
int t = 0;
while (t < s.size()) {
if (s[t] == '/') { t++; continue; }
int k = string(s, t, 1000).find('/');
k = (k == string::npos) ? 1000 : k;
string s1(s, t, k);
t += k;
if (s1 == "..") { if (d.size()) d.pop_back(); }
else if(s1!=".") d.push_back(s1);
}
}
void printpath(vector<string> &d) {
if (d.empty()) cout << "/" ;
for (int i = 0; i < d.size(); ++i)  cout << "/" << d[i];
cout << endl;
}
int main() {
int n;
cin >> n;
string s;
getchar();
getline(cin, s);
f(s, dir);
vector<string> tmp;
while (n--) {
tmp.clear();
getline(cin, s);
if (s.empty()) { printpath(dir); continue; }
if (s[0] != '/') tmp = dir;
f(s, tmp);
printpath(tmp);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: