美图c++工程师
发布于 2017-04-16 21:12 1582 次浏览 0 赞 来自 笔试面试  

//简化途径path的,通过67%

string simplify(string path) {


char output[1000];

if (path.empty()){

return output;

}

int len = path.size();

stack<char> s1;

stack<char> s2;


for (int i = 0; i < len; i++){

if (!s1.empty() && path[i] == '.'&&path[++i] == '.'){

s1.pop();

}

if (path[i] >= 'a'&&path[i] <= 'z'){

s1.push(path[i]);

}

}

while (s1.size()){

char tmp = s1.top();

s1.pop();

s2.push(tmp);

}

int i = 0;

while (s2.size()){

char tmp = s2.top();

if (i == 0){

if (path[0] == '/'){

output[i++] = '/';

output[i++] = tmp;

}

else{

output[i++] = tmp;

}

}

else{

output[i++] = '/';

output[i++] = tmp;

}

s2.pop();

}

output[i] = '\0';

return output;

}


int main() {

string res;

string _path;

getline(cin, _path);

res = simplify(_path);

cout << res << endl;

system("pause");

return 0;

}

//另外一题,二叉搜索树查找,没看明白题目的意思。


添加回复
回到顶部