The_Law

Untitled

Jan 16th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector< vector<int> > let;
  6. vector<string> ans;
  7.  
  8. bool check(int i, int it)
  9. {
  10.     vector<int> curr = let[i];
  11.  
  12.     bool pref = true;
  13.  
  14.     for (int j = 0; j < it && pref; ++j)
  15.     {
  16.         if (curr[ans[i - 1][j] - 'a'] > 0)
  17.             --curr[ans[i - 1][j] - 'a'];
  18.         else
  19.             pref = false;
  20.     }
  21.  
  22.     if (!pref)
  23.         return false;
  24.  
  25.     for (int j = it; j < ans[i - 1].size(); ++j)
  26.     {
  27.         int more = -1;
  28.  
  29.         for (int l = ans[i - 1][j] - 'a' + 1; l < let[i].size(); ++l)
  30.             if (curr[l] > 0)
  31.                 more = l;
  32.  
  33.         if (more != -1)
  34.             return true;
  35.  
  36.         if (curr[ans[i - 1][j] - 'a'] <= 0)
  37.                 return false;
  38.  
  39.         --curr[ans[i - 1][j] - 'a'];
  40.     }
  41.  
  42.     return true;
  43. }
  44.  
  45. void build(int i, int it)
  46. {
  47.     for (int l = 0; l < it; ++l)
  48.         ans[i] += ans[i - 1][l], --let[i][ans[i - 1][l] - 'a'];
  49.  
  50.     int more = -1;
  51.  
  52.     for (int l = ans[i - 1][it] - 'a' + 1; l < let[i].size(); ++l)
  53.         if (let[i][l] > 0)
  54.         {
  55.             more = l;
  56.             break;
  57.         }
  58.  
  59.     if (more != -1 && let[i][more] > 0)
  60.     {
  61.         ans[i] += more + 'a';
  62.         --let[i][more];
  63.     }
  64.     else if (let[i][ans[i - 1][it]] > 0)
  65.     {
  66.         ans[i] += ans[i - 1][it] + 'a';
  67.         --let[i][ans[i - 1][it]];
  68.     }
  69.  
  70.  
  71.     for (int l = 0; l < let[i].size(); ++l)
  72.         for (int k = 0; k < let[i][l]; ++k)
  73.             ans[i] += l + 'a';
  74. }
  75.  
  76. int main()
  77. {
  78. //    freopen("A.in", "r", stdin);
  79.  
  80.     int n;
  81.     cin >> n;
  82.  
  83.     let.resize(n, vector<int> (26, 0));
  84.     ans.resize(n);
  85.  
  86.     for (int i = 0; i < n; ++i)
  87.     {
  88.         string curr;
  89.         cin >> curr;
  90.  
  91.         if (i == 0)
  92.             ans[0] = curr, sort(ans[i].begin(), ans[i].end());
  93.         else
  94.             for (int it = 0; it < curr.size(); ++it)
  95.                 ++let[i][curr[it] - 'a'];
  96.     }
  97.  
  98.     bool bad = false;
  99.  
  100.  
  101.     for (int i = 1; i < n && !bad; ++i)
  102.     {
  103.         int l = 0;
  104.         int r = ans[i - 1].size() + 1;
  105.  
  106.         while(r - l != 1)
  107.         {
  108.             int m = l + (r - l) / 2;
  109.  
  110.             if (check(i, m))
  111.                 l = m;
  112.             else
  113.                 r = m;
  114.         }
  115.  
  116.         if (check(i, l))
  117.         {
  118.             build(i, l);
  119.         }
  120.         else
  121.             bad = true;
  122.     }
  123.  
  124.     if (bad)
  125.         cout << -1;
  126.     else
  127.         for (int i = 0; i < n; ++i)
  128.             cout << ans[i] << endl;
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment