/*
* ## ##### #### #### # # ####
* # # # # # # # # # # # #
* # # ##### # # # # # # #
* ###### # # # # # # ## # # #
* # # # # # # # # ## ## # #
* # # ##### #### #### # # ####
*/
#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(n * m + 1, 1), pwinv(n * m + 1, 1);
for (int i = 1; i <= 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 + 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 + 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 |
332 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 |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
35 ms |
2764 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
36 ms |
2852 KB |
Output is correct |
12 |
Correct |
40 ms |
2960 KB |
Output is correct |
13 |
Correct |
37 ms |
2940 KB |
Output is correct |
14 |
Correct |
37 ms |
2992 KB |
Output is correct |
15 |
Correct |
36 ms |
3012 KB |
Output is correct |
16 |
Correct |
37 ms |
2968 KB |
Output is correct |
17 |
Correct |
45 ms |
2980 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 |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
35 ms |
2764 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
36 ms |
2852 KB |
Output is correct |
12 |
Correct |
40 ms |
2960 KB |
Output is correct |
13 |
Correct |
37 ms |
2940 KB |
Output is correct |
14 |
Correct |
37 ms |
2992 KB |
Output is correct |
15 |
Correct |
36 ms |
3012 KB |
Output is correct |
16 |
Correct |
37 ms |
2968 KB |
Output is correct |
17 |
Correct |
45 ms |
2980 KB |
Output is correct |
18 |
Correct |
449 ms |
29784 KB |
Output is correct |
19 |
Correct |
7 ms |
844 KB |
Output is correct |
20 |
Correct |
7 ms |
844 KB |
Output is correct |
21 |
Correct |
441 ms |
29960 KB |
Output is correct |
22 |
Correct |
469 ms |
29960 KB |
Output is correct |
23 |
Correct |
433 ms |
29860 KB |
Output is correct |
24 |
Correct |
472 ms |
29892 KB |
Output is correct |
25 |
Correct |
435 ms |
29852 KB |
Output is correct |
26 |
Correct |
465 ms |
29856 KB |
Output is correct |
27 |
Correct |
485 ms |
29828 KB |
Output is correct |