Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #ifndef d
  4. #define d(...)
  5. #endif
  6. #define st first
  7. #define nd second
  8. #define pb push_back
  9. #define siz(c) (int)(c).size()
  10. #define all(c) (c).begin(), (c).end()
  11. typedef long long LL;
  12. typedef long double LD;
  13. constexpr int INF=1e9+7;
  14. constexpr LL INFL=1e18;
  15. template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  16.   return os << "(" << P.st << "," << P.nd << ")";
  17. }
  18. template<class T> ostream &operator<<(ostream &os, const vector<T> &V) {
  19.     os << "[";
  20.     for(const auto& x:V)
  21.         os << x << ", ";
  22.     return os << "]";
  23. }
  24.  
  25. constexpr int maxn = 1 << 10;
  26.  
  27. int n;
  28. string M[maxn];
  29. int pos[maxn][maxn];
  30.  
  31.  
  32. int main()
  33. {
  34.     ios_base::sync_with_stdio(0);
  35.     cin.tie(0);
  36.    
  37.     string tmp; cin >> tmp;
  38.     n = tmp.size();
  39.     M[0] = tmp;
  40.     for(int i=1; i<n; i++)
  41.         cin >> M[i];
  42.    
  43.     for(int i=0; i<n; i++) {
  44.         string cur = M[i] + M[i];
  45.         for(size_t j=0; j<cur.size(); j++)
  46.             pos[i][j] = cur[j] - 'a';
  47.         for(int len = 2; len * 2 < n; len <<= 1) {
  48.             vector<pair<pair<int, int>, int>> v;
  49.             for(int j=0; j<cur.size(); j++) {
  50.                 v.push_back({{pos[i][j], j + len >= cur.size() ? cur.size() : pos[i][j + len]}, j});
  51.             }
  52.            
  53.             sort(all(v));
  54.             for(size_t a=0, b=0, cnt=0; a<v.size(); a=b, cnt++) {
  55.                 while(b < v.size() and v[a].st == v[b].st)
  56.                     pos[i][v[b++].nd] = cnt;
  57.             }
  58.         }
  59.        
  60.         vector<pair<pair<int, int>, int>> v;
  61.         for(int j=0; j<cur.size()/2; j++) {
  62.             v.push_back({{pos[i][j], pos[i][j + cur.size()/4]}, j});
  63.         }
  64.         sort(all(v));
  65.         for(size_t a=0, b=0, cnt=0; a<v.size(); a=b, cnt++) {
  66.                 while(b < v.size() and v[a].st == v[b].st)
  67.                     pos[i][v[b++].nd] = cnt;
  68.             }
  69.        
  70.         for(int j=0; j<n; j++)
  71.             cout << pos[i][j] << " ";
  72.         cout << endl;
  73.     }
  74.    
  75.     vector<pair<int, int>> cand;
  76.    
  77.     for(int i=0; i<n; i++) {
  78.         vector<int> cur(n);
  79.         iota(all(cur), 0);
  80.         for(int j=0; j<n; j++) {
  81.             int best = INF;
  82.             vector<int> nxt;
  83.             for(auto k:cur) {
  84.                 int x = i + j, y = k;
  85.                 if(x >= n) x -= n;
  86.                 if(pos[x][y] < best) {
  87.                     best = pos[x][y];
  88.                     nxt.clear();
  89.                 }
  90.                
  91.                 if(pos[x][y] == best)
  92.                     nxt.push_back(k);
  93.             }
  94.             cur = nxt;
  95.         }
  96.         assert(not cur.empty());
  97.         cand.emplace_back(i, cur[0]);
  98.     }
  99.    
  100.     auto get = [&](int x, int y, int add) {
  101.         x += add / n;
  102.         y += add % n;
  103.         if(x >= n) x -= n;
  104.         if(y >= n) y -= n;
  105.         return M[x][y];
  106.     };
  107.    
  108.     for(int i=0; i<n*n and cand.size() > 1; i++) {
  109.         vector<pair<int, int>> nxt;
  110.         char best = 'z';
  111.         for(auto p:cand) {
  112.             auto c = get(p.st, p.nd, i);
  113.             if(c < best) {
  114.                 best = c;
  115.                 nxt.clear();
  116.             }
  117.             if(c == best)
  118.                 nxt.emplace_back(p);
  119.         }
  120.     }
  121.    
  122.     int x, y;
  123.     tie(x, y) = cand[0];
  124.     for(int i=0; i<n*n; i++) {
  125.         if(i % n == 0 and i > 0) cout << "\n";
  126.         cout << get(x, y, i);
  127.     }
  128.     cout << "\n";
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement