题目的源地址: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;
}