/*
* ## ##### #### #### # # ####
* # # # # # # # # # # # #
* # # ##### # # # # # # #
* ###### # # # # # # ## # # #
* # # # # # # # # ## ## # #
* # # ##### #### #### # # ####
*/
#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()) 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 = 1e9 + 7, N = 200001, logN = 20, K = 48763;
int main () {
owo;
int n, m;
cin >> n >> m;
vector <int> id(n, 0), pw(2 * m + 1, 1);
for (int i = 1; i <= 2 * m; ++i) pw[i] = 1ll * pw[i - 1] * K % mod;
vector <string> s(n);
vv <int> pre(n, 2 * m + 1, 0);
auto query = [&](int i, int l, int r) {
return (pre[i][r] - 1ll * pre[i][l] * pw[r - l] % mod + mod) % mod;
};
for (int i = 0; i < n; ++i) {
cin >> s[i], s[i] += s[i];
for (int j = 0; j < 2 * m; ++j) {
pre[i][j + 1] = (1ll * pre[i][j] * K + s[i][j]) % mod;
}
for (int j = 1; j < m; ++j) {
int l = 0, r = m;
while (r - l > 1) {
int mid = l + r >> 1;
if (query(i, id[i], id[i] + mid) == query(i, j, j + mid)) l = mid;
else r = mid;
}
if (s[i][id[i] + l] > s[i][j + l]) {
id[i] = j;
}
}
}
int ansr = 0, ansc = id[0];
for (int i = 1; i < n; ++i) {
int row = -1;
for (int j = 0; j < n; ++j) {
if (query((ansr + j) % n, ansc, ansc + m) != query((i + j) % n, id[i], id[i] + m)) {
row = j;
break;
}
}
if (row == -1) continue;
int l = 0, r = m;
while (r - l > 1) {
int mid = l + r >> 1;
if (query((ansr + row) % n, ansc, ansc + mid) == query((i + row) % n, id[i], id[i] + mid)) l = mid;
else r = mid;
}
if (s[(ansr + row) % n][ansc + l] > s[(i + row) % n][id[i] + l]) {
ansr = i, ansc = id[i];
}
}
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
cout << s[(ansr + i) % n][ansc + j];
if (j == m - 1) cout << '\n';
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:76:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:97:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | 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 |
16 ms |
1196 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
17 ms |
1228 KB |
Output is correct |
12 |
Incorrect |
19 ms |
1220 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
16 ms |
1196 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
17 ms |
1228 KB |
Output is correct |
12 |
Incorrect |
19 ms |
1220 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |