CSP 201604-3 路径解析

一、问题链接

CSP 201604-3 路径解析

二、参考代码

#include <bits/stdc++.h>
using namespace std;
//以字符c来分割字符串 
vector<string> split(string s,char c) {
	vector<string> res;
	stringstream ss(s);
	string temp;
	while(getline(ss,temp,c)) {
		res.push_back(temp);
	}
	return res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int p;
	string cur;
	cin>>p>>cur;
	//吸收回车,防止影响后续的getline()
	cin.get();
	while(p--) {
		string s;
		//可能存在空行的输入,用getline()
		getline(cin,s);
		vector<string> ans;
		if(!s.empty() and s[0]!='/') { //相对路径
			s=cur+'/'+s;//待处理的绝对路径绝对路径
		} else if(s.empty()) {
			cout<<cur<<"\n";
			continue;
		}
		vector<string> v=split(s,'/');
		for(auto &i:v) {
			//遇到. 或 空 或 已经到达根目录则不操作 
			if(i=="." or i.empty() or (ans.empty() and i=="..")) continue;
			//返回上一层目录
			else if(i=="..") ans.pop_back();
			else ans.push_back(i);
		}
		//位于根目录
		if(ans.empty()) {
			cout<<"/"<<"\n";
			continue;
		}
		for(auto &i:ans) {
			cout<<"/"<<i;
		}
		cout<<"\n";
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值