题目1090:路径打印

题目的源地址:http://ac.jobdu.com/problem.php?pid=1090

C++代码:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
vector<string> v;
bool cmp(string a,string b)
{
	int len=a.length()>b.length()?b.length():a.length();
	for(int i=0;i<len;i++)
	{
		if(a[i]!=b[i])
		{
			if(a[i]!='\\'&&b[i]!='\\')
			{
				return a[i]<b[i];
			}
			else if(a[i]=='\\')
			{
				return true;
			}
			else if(b[i]=='\\')
				return false;
		}
	}
	return a.length()<b.length();
}
int main()
{
	int n;string str;
	while(cin>>n)
	{
		if(n==0)
			return 0;
		v.resize(0);
		while(n--)
		{
			cin>>str;
			if(str[str.length()-1]=='\\')
				str.erase(str.length()-1,str.length());//删除从开始位置到结束位置的字符
			if(find(v.begin(),v.end(),str)==v.end())
				v.push_back(str);
			while(str.find('\\')!=string::npos)
			{
				str.erase(str.find_last_of('\\'),str.length());
				if(find(v.begin(),v.end(),str)==v.end())
					v.push_back(str);
			}
		}
		sort(v.begin(),v.end(),cmp);
		for(int i=0;i<v.size();i++)
		{
			if(v[i].find('\\')==string::npos)
				cout<<v[i]<<endl;
			else
			{
				for(int j=0;j<v[i].find_last_of('\\');j++)
					cout<<" ";
				cout<<" ";
				cout<<v[i].substr(v[i].find_last_of('\\')+1,v[i].length())<<endl;
			}
		}
		cout<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值