时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:1947
解决:377
- 题目描述:
-
给你一串路径,譬如:
ac
ade
bcst
d
你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右缩一格,就像这样:
a
b
c
d
e
b
cst
d
同一级的需要按字母顺序排列,不能乱。
- 输入:
-
每个测试案例第一行为一个正整数n(n<=10)表示有n个路径,当n为0时,测试结束,接下来有n行,每行有一个字串表示一个路径,长度小于50。
- 输出:
-
输出目录结构,每一个测试样例的输出紧跟一个空行。
- 样例输入:
-
4 ac ade bcst d 0
- 样例输出:
-
a b c d e b cst d
可以利用STL可以实现,无需用N叉树。
//Asimple #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cctype> #include <cstdlib> #include <stack> #include <cmath> #include <set> #include <map> #include <string> #include <queue> #include <limits.h> #define INF 0x7fffffff #define mod 1000000000 using namespace std; const int maxn = 55; typedef long long ll; 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; }