/*
* ## ##### #### #### # # ####
* # # # # # # # # # # # #
* # # ##### # # # # # # #
* ###### # # # # # # ## # # #
* # # # # # # # # ## ## # #
* # # ##### #### #### # # ####
*/
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define info() cout << __PRETTY_FUNCTION__ << ": " << __LINE__ << endl
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
if (a.empty()) return o << "{}";
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
vv(int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
vv() {}
};
template <typename T> struct vvv : vector <vv <T>> {
vvv(int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
vvv() {}
};
#ifdef Doludu
#define test(args...) info(), abc("[" + string(#args) + "]", args)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 998244353, N = 1000001, logN = 20, K = 48763, KK = 331983854;
int main () {
owo;
int n, m;
cin >> n >> m;
vector <int> pw(4 * n * m + 1, 1), pwinv(4 * n * m + 1, 1);
for (int i = 1; i <= 4 * n * m; ++i) {
pw[i] = 1ll * pw[i - 1] * K % mod;
pwinv[i] = 1ll * pwinv[i - 1] * KK % mod;
}
vector <string> s(n * 2);
vv <int> pre(2 * n + 1, 2 * m + 1, 0);
auto query = [&](int i, int j, int l, int r) {
lli ans = ((long long)pre[j][r] - pre[j][l] - pre[i][r] + pre[i][l]) % mod;
if (ans < 0) ans += mod;
return ans * pwinv[i * m + l] % mod;
};
for (int i = 0; i < n; ++i) {
cin >> s[i], s[i] += s[i], s[n + i] = s[i];
}
for (int i = 0; i < 2 * n; ++i) {
int sum = 0;
for (int j = 0; j < 2 * m; ++j) {
sum += 1ll * s[i][j] * pw[i * m + j] % mod;
if (sum >= mod) {
sum -= mod;
}
pre[i + 1][j + 1] = pre[i][j + 1] + sum;
if (pre[i + 1][j + 1] >= mod) {
pre[i + 1][j + 1] -= mod;
}
}
}
int ansl = 0, ansr = 0;
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
if (!i && !j) continue;
int l = 0, r = n + 1;
while (r - l > 1) {
int mid = l + r >> 1;
if (query(ansl, ansl + mid, ansr, ansr + m) == query(i, i + mid, j, j + m)) {
l = mid;
} else {
r = mid;
}
}
if (l == n) continue;
int mx = l;
l = 0, r = m;
while (r - l > 1) {
int mid = l + r >> 1;
if (query(ansl + mx, ansl + mx + 1, ansr, ansr + mid) == query(i + mx, i + mx + 1, j, j + mid)) {
l = mid;
} else {
r = mid;
}
}
if (s[ansl + mx][ansr + l] > s[i + mx][j + l]) {
ansl = i, ansr = j;
}
}
for (int i = ansl; i < ansl + n; ++i) {
for (int j = ansr; j < ansr + m; ++j) {
cout << s[i][j];
}
cout << endl;
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:94:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:105:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
105 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
49 ms |
4800 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
41 ms |
4792 KB |
Output is correct |
12 |
Correct |
40 ms |
4784 KB |
Output is correct |
13 |
Correct |
41 ms |
4956 KB |
Output is correct |
14 |
Correct |
48 ms |
4944 KB |
Output is correct |
15 |
Correct |
41 ms |
4964 KB |
Output is correct |
16 |
Correct |
42 ms |
4940 KB |
Output is correct |
17 |
Correct |
43 ms |
5092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
49 ms |
4800 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
41 ms |
4792 KB |
Output is correct |
12 |
Correct |
40 ms |
4784 KB |
Output is correct |
13 |
Correct |
41 ms |
4956 KB |
Output is correct |
14 |
Correct |
48 ms |
4944 KB |
Output is correct |
15 |
Correct |
41 ms |
4964 KB |
Output is correct |
16 |
Correct |
42 ms |
4940 KB |
Output is correct |
17 |
Correct |
43 ms |
5092 KB |
Output is correct |
18 |
Correct |
504 ms |
52212 KB |
Output is correct |
19 |
Correct |
6 ms |
972 KB |
Output is correct |
20 |
Correct |
8 ms |
1304 KB |
Output is correct |
21 |
Correct |
480 ms |
52368 KB |
Output is correct |
22 |
Correct |
521 ms |
52376 KB |
Output is correct |
23 |
Correct |
494 ms |
52364 KB |
Output is correct |
24 |
Correct |
539 ms |
52488 KB |
Output is correct |
25 |
Correct |
501 ms |
52376 KB |
Output is correct |
26 |
Correct |
525 ms |
52376 KB |
Output is correct |
27 |
Correct |
637 ms |
52360 KB |
Output is correct |