Advertisement
Hippskill

Untitled

Jan 27th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<sstream>
  10. #include<cstring>
  11. #include<numeric>
  12. #include<limits.h>
  13. #include<time.h>
  14. using namespace std;
  15.  
  16.  
  17.  
  18. struct item {
  19.     string name;
  20.     map<string, item> childs;
  21.     item(){}
  22.     item(string name): name(name) {}
  23. };
  24.  
  25. map<string, item> roots;
  26.  
  27. const int SZ = 100;
  28.  
  29. char p[SZ];
  30.  
  31. void add(item & cur, vector<string> & path, int stage) {
  32.     if (stage >= path.size())
  33.         return;
  34.     else {
  35.         if (cur.childs.count(path[stage]) == 0) {
  36.             cur.childs[path[stage]] = item(path[stage]);
  37.         }
  38.         add(cur.childs[path[stage]], path, stage + 1);
  39.     }
  40. }
  41.  
  42. void add(vector<string> & path) {
  43.     if (roots.count(*path.begin()) == 0) {
  44.         roots[*path.begin()] = item(*path.begin());
  45.     }
  46.     add(roots[*path.begin()], path, 1);
  47. }
  48.  
  49. void print(item cur, int fl) {
  50.     for (int i = 0; i < fl; i++) {
  51.         printf(" ");
  52.     }
  53.     printf("%s\n", cur.name.c_str());
  54.     for (auto i : cur.childs) {
  55.         print(i.second, fl + 1);
  56.     }
  57. }
  58.  
  59. int main() {
  60. #ifndef ONLINE_JUDGE
  61.     freopen("input.txt", "r", stdin);
  62. #endif
  63.     int n;
  64.     scanf("%d", &n);
  65.     for (int i = 0; i < n; i++) {
  66.         scanf("%s", p);
  67.         vector<string> path;
  68.         int len = strlen(p);
  69.         int prev = 0;
  70.         for (int j = 0; j < len; j++) {
  71.             if (p[j] == '\\') {
  72.                 path.push_back(string(p + prev, p + j - 1));
  73.                 prev = j + 1;
  74.             }
  75.         }
  76.         path.push_back(string(p + prev, p + len));
  77.         add(path);
  78.     }
  79.     for (auto i : roots) {
  80.         print(i.second, 0);
  81.     }
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement