제출 #1157560

#제출 시각아이디문제언어결과실행 시간메모리
1157560chaoslongSateliti (COCI20_satellti)C++17
10 / 110
17 ms9796 KiB
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "SATELITI"

using namespace std;
const int N = 3000;
const ll mod = 1999999927; // 998244353
const int base = 311;
const ll oo = 1e18;

int n, m;
ll p[N], h[N][N], c[N][N];
char s[N][N];

ll get(int id, int l, int r) {
    return (h[id][r] - h[id][l-1] * p[r - l + 1] + mod * mod) % mod;
}

ll get2(int id, int l, int r) {
    return (c[id][r] - c[id][l-1] * p[r - l + 1] + mod * mod) % mod;
}

void to_nho_cau() {
    cin >> n >> m;
    forr(i, 1, n) {
        forr(j, 1, m) {
            cin >> s[i][j];
        }
    }
    forr(i, n+1, 2 * n) {
        forr(j, 1, m) {
            s[i][j] = s[i-n][j];
        }
    }
    forr(i, 1, n) {
        forr(j, m + 1, 2 * m) {
            s[i][j] = s[i][j-m];
        }
    }
    forr(i, n+1, 2 * n) {
        forr(j, m+1, 2 * m) {
            s[i][j] = s[i-n][j-m];
        }
    }
//    forr(i, 1, 2 * n) {
//        forr(j, 1, 2 * m) {
//            cout << s[i][j] << " ";
//        }
//        cout << "\n";
//    }
    //base^i
    p[0] = 1;
    forr(i, 1, m) p[i] = (p[i-1] * base) % mod;
    //hash
    forr(i, 1, 2 * n) {
        forr(j, 1, 2 * m) {
            h[i][j] = (h[i][j-1] * base + s[i][j]) % mod;
        }
        //hash cai table tu hang i cot j do dai m
        forf(j, 0, m) {
            c[j][i] = (c[j][i-1] * base + get(i, j, j + m - 1)) % mod;
        }
    }
//    cout << get2(2, 3, 3 + 1) << "\n";
//    cout << get2(3, 3, 3 + 1) << "\n";
    pii mn = mp(1, 1);
    forr(i, 1, n) {
        forr(j, 1, m) {
            int l = 0, r = n-1, pos = -1;
            while(l <= r) {
                int mid = (l + r) >> 1;
                ll cur1 = get2(mn.nd, mn.st, mn.st + mid);
                ll cur2 = get2(j, i, i + mid);
                if(cur1 != cur2) {
                    pos = mid;
                    r = mid - 1;
                } else l = mid + 1;
            }
            if(pos == -1) continue;
            l = 0, r = m-1; int ans = -1;
            while(l <= r) {
                int mid = (l + r) >> 1;
                ll cur1 = get(mn.st, mn.nd, mn.nd + mid);
                ll cur2 = get(i, j, j + mid);
                if(cur1 != cur2) {
                    ans = mid;
                    r = mid - 1;
                } else l = mid + 1;
            }
            if(s[mn.st + pos][mn.nd + ans] > s[i + pos][j + ans]) {
//            cout << i << " " << j << " " << pos << " " << ans << "\n";
                mn = mp(i, j);
            }
        }
    }
//    cout << mn.st << " " << mn.nd << "\n";
    forr(i, mn.st, mn.st + n - 1) {
        forr(j, mn.nd, mn.nd + m - 1) {
            cout << s[i][j];
        }
        cout << "\n";
    }
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    #ifdef LOCAL
        freopen(file".inp","r",stdin);
        freopen(file".out","w",stdout);
    #endif
    int t = 1;
    //cin >> t;
    while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...