// 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
forr(j, 1, m) {
c[j][i] = (c[j][i-1] * base + get(i, j, j + m - 1)) % mod;
}
}
pii mn = mp(1, 1);
forr(i, 1, n) {
forr(j, 1, m) {
int l = 1, r = n, 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);
// cout << l << " " << r << " " << mid << " " << cur1 << " " << cur2 << endl;
if(cur1 != cur2) {
pos = mid;
r = mid - 1;
} else l = mid + 1;
}
if(pos == -1) continue;
l = 1, r = m; int ans = -1;
while(l <= r) {
int mid = (l + r) >> 1;
ll cur1 = get(mn.st + pos - 1, mn.nd, mn.nd + mid - 1);
ll cur2 = get(i + pos - 1, j, j + mid - 1);
if(cur1 != cur2) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
if(s[mn.st + pos - 1][mn.nd + ans - 1] > s[i + pos - 1][j + ans - 1]) {
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |