Advertisement
KiK0S

bohr_1

Dec 4th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int k = 26;
  6. const int maxn = 15e5;
  7.  
  8. struct vert{
  9.     int flag = 0;
  10.     int go[k];
  11. };
  12.  
  13. vert g[maxn];
  14. int all = 1;
  15. void addstr(string s){
  16.     int v = 0;
  17.     for(int i = 0; i < s.size(); i++){
  18.         int to = s[i] - 'a';
  19.         if(g[v].go[to] == -1){
  20.             g[v].go[to] = all++;
  21.             for(int q = 0; q < k; q++){
  22.                 g[all-1].go[q] = -1;
  23.             }
  24.         }
  25.         v = g[v].go[to];
  26.     }
  27.     g[v].flag++;
  28. }
  29.  
  30.  
  31. string cur;
  32. vector<string> sortedans;
  33.  
  34. void print(int v){
  35.     for(int i = 0; i < g[v].flag; i++){
  36.         sortedans.push_back(cur);
  37.     }
  38.     for(int i = 0; i < k; i++){
  39.         if(g[v].go[i] != -1){
  40.             cur.push_back(i + 'a');
  41.             print(g[v].go[i]);
  42.             cur.pop_back();
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. signed main(){
  49.     ios_base::sync_with_stdio(0);
  50.     cin.tie(0);
  51.     cout.tie(0);
  52.     string s;
  53.     cin>>s;
  54.     for(int q = 0; q < k; q++){
  55.         g[0].go[q] = -1;
  56.     }
  57.     vector<int> points;
  58.     int row = 0;
  59.     for(int i = 0; i < s.size();){
  60.         if(s[i] == '.'){
  61.             row++;
  62.             i++;
  63.         }
  64.         else{
  65.             points.push_back(row);
  66.             row = 0;
  67.             int len = 0;
  68.             while(i < s.size() && s[i] != '.'){
  69.                 i++;len++;
  70.             }
  71.             addstr(s.substr(i - len, len));
  72.         }
  73.     }
  74.     points.push_back(row);
  75.     print(0);
  76.     for(int i = 0; i < points.size(); i++){
  77.         for(int j = 0; j < points[i]; j++){
  78.             cout<<'.';
  79.         }
  80.         if(i < sortedans.size())cout<<sortedans[i];
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement