(字典树)Hihocoder - 1383 The Book List

原题链接:http://hihocoder.com/problemset/problem/1383t


 

题意:给定一些文件的绝对路径,然后把整个主目录按照树形结构输出。


 

分析:

字典树,用Map,从从字符串映射到下个节点,

注意:如果出现

a/b/c

a/b

这种应该输出:

a

  b

    c

  b

因为b既是文件又是目录,

妈的,在这里坑了三个小时

所有目录放在文件前面输出。

目录之间和文件之间分别按字典序排序。

最后递归输出。


 

代码:

  1 #include <set>
  2 #include <map>
  3 #include <list>
  4 #include <cmath>
  5 #include <queue>
  6 #include <vector>
  7 #include <bitset>
  8 #include <string>
  9 #include <cctype>
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 
 16 using namespace std;
 17 
 18 typedef long long ll;
 19 typedef unsigned long long ull;
 20 #define inf (0x3f3f3f3f)
 21 #define lnf (0x3f3f3f3f3f3f3f3f)
 22 #define eps (1e-8)
 23 int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;}
 24 
 25 //--------------------------
 26 
 27 string str;
 28 
 29 struct Node{
 30     string name;
 31     bool exist;
 32     map<string,struct Node*> id;
 33     Node(string name){
 34         exist=false;
 35         name=name;
 36         id.clear();
 37     }
 38     Node(){
 39         id.clear();
 40         exist=false;
 41     };
 42     ~Node(){
 43         id.clear();
 44         name.clear();
 45     }
 46 };
 47 
 48 
 49 struct Node *root;
 50 
 51 string space="    ";
 52 
 53 void node_insert(string &s){
 54     struct Node *rt=root;
 55     string tmp;
 56     for(int i=0;i<s.length();i++){
 57         if(s[i]=='/'){
 58             if(!rt->id[tmp]){
 59                 rt->id[tmp]=new Node(tmp);
 60             }
 61             rt=rt->id[tmp];
 62             tmp.clear();
 63             continue;
 64         }
 65         tmp.push_back(s[i]);
 66     }
 67     if(!rt->id[tmp]){
 68         rt->id[tmp]=new Node(tmp);
 69     }
 70     rt=rt->id[tmp];
 71     rt->exist=true;
 72 }
 73 
 74 
 75 
 76 bool cmp(map<string,struct Node*>::iterator a ,map<string,struct Node*>::iterator b){
 77     return (*a).first<(*b).first;
 78 }
 79 
 80 void node_print(struct Node *rt,string k){
 81     if(rt->id.size()==0)return;
 82     vector<map<string,struct Node*>::iterator> vec1,vec2;
 83     for(map<string,struct Node*>::iterator it=rt->id.begin();it!=rt->id.end();it++){
 84         if((*it).second->exist)vec2.push_back(it);
 85         if((*it).second->id.size()>0)vec1.push_back(it);
 86     }
 87     sort(vec1.begin(),vec1.end(),cmp);
 88     sort(vec2.begin(),vec2.end(),cmp);
 89     for(vector<map<string,struct Node*>::iterator>::iterator it=vec1.begin();it!=vec1.end();it++){
 90         cout<<k<<(**it).first<<endl;
 91         node_print(rt->id[(**it).first],k+space);
 92     }
 93     for(vector<map<string,struct Node*>::iterator>::iterator it=vec2.begin();it!=vec2.end();it++){
 94         cout<<k<<(**it).first<<endl;
 95     }
 96 }
 97 
 98 void rm(struct Node *rt){
 99     for(map<string,struct Node*>::iterator it=rt->id.begin();it!=rt->id.end();it++){
100         rm((*it).second);
101     }
102     delete rt;
103 }
104 
105 void init(){
106     root=new Node();
107 }
108 
109 
110 
111 
112 void solve(){
113     init();
114     int t=0;
115     while(getline(cin,str)){
116         if(str=="0"){
117             cout<<"Case "<<++t<<":"<<endl;
118             node_print(root,"");
119             rm(root);
120             init();
121             continue;
122         }
123         node_insert(str);
124     }
125 }
126 
127 
128 
129 
130 
131 int main() {
132 
133 #ifndef ONLINE_JUDGE
134     freopen("in.txt", "r", stdin);
135     freopen("out.txt", "w", stdout);
136 #endif
137     iostream::sync_with_stdio(false);
138     solve();
139     return 0;
140 }

 

posted @ 2016-09-24 18:31  tak_fate  阅读(296)  评论(0编辑  收藏  举报