/*
* ## ##### #### #### # # ####
* # # # # # # # # # # # #
* # # ##### # # # # # # #
* ###### # # # # # # ## # # #
* # # # # # # # # ## ## # #
* # # ##### #### #### # # ####
*/
#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, mod2 = 998244353;
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] = pw[i - 1] + pw[i - 1];
if (pw[i] >= mod) pw[i] -= mod;
}
vector <string> s(n);
vv <int> pre(n, 2 * m + 1, 0), pre2(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;
};
auto query2 = [&](int i, int l, int r) {
return (pre2[i][r] - 1ll * pre2[i][l] * pw[r - l] % mod + mod) % mod;
};
for (int i = 0; i < n; ++i) {
cin >> s[i], s[i] += s[i];
for (char &c : s[i]) {
if (c == '*') c = '0';
else c = '1';
}
for (int j = 0; j < 2 * m; ++j) {
pre[i][j + 1] = (1ll * pre[i][j] * 2 + s[i][j] - '0') % mod;
pre2[i][j + 1] = (1ll * pre2[i][j] * 2 + s[i][j] - '0') % 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) && query2(i, id[i], id[i] + mid) == query2(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) && query2((ansr + j) % n, ansc, ansc + m) != query2((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) && query2((ansr + row) % n, ansc, ansc + mid) == query2((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] == '0' ? '*' : '.');
if (j == m - 1) cout << '\n';
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:87:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:108:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
14 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
24 ms |
1892 KB |
Output is correct |
12 |
Incorrect |
25 ms |
1912 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
14 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
24 ms |
1892 KB |
Output is correct |
12 |
Incorrect |
25 ms |
1912 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |