#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define pii pair<int, int>
#define fi first
#define se second
#define sz(x) (int)(x).size()
const int M = 1e9 + 9;
random_device rd;
const int B = uniform_int_distribution<>(31, 1000)(rd);
struct Hashing{
vi phash, pw{1ll};
Hashing(const vi &s) : phash(sz(s) + 1){
pw.resize(sz(s) + 1);
for(int i = 1; i <= sz(s); i++){
pw[i] = (pw[i - 1] * B) % M;
phash[i] = (phash[i - 1] * B + s[i - 1]) % M;
}
}
inline int get_hash(int l, int r){
int ret = phash[r + 1] - ((phash[l] * pw[r - l + 1]) % M);
return (ret < 0 ? ret + M : ret);
}
};
signed main(){
//ios_base::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> word(2 * n);
for(int i = 0; i < n; i++){
cin >> word[i];
}
for(int i = n; i < 2 * n; i++){
word[i - n] += word[i - n];
word[i] = word[i - n];
}
vector<Hashing> hash, hash_hash;
for(int i = 0; i < 2 * n; i++){
hash.emplace_back(vi(word[i].begin(), word[i].end()));
}
for(int j = 0; j < m; j++){
vi s;
for(int i = 0; i < 2 * n; i++){
s.emplace_back(hash[i].get_hash(j, j + m - 1));
}
hash_hash.emplace_back(s);
}
auto is_better = [&](const pii &a, const pii &b) -> bool {
int lo = 0, hi = n, ret = 0, mid, h1, h2;
while(hi >= lo){
mid = lo + (hi - lo) / 2;
h1 = hash_hash[a.se].get_hash(a.fi, a.fi + mid - 1), h2 = hash_hash[b.se].get_hash(b.fi, b.fi + mid - 1);
if(h1 == h2){
lo = mid + 1;
ret = mid;
}
else{
hi = mid - 1;
}
}
if(ret == n) return false;
int frst = ret;
lo = 0, hi = m, ret = 0;
while(hi >= lo){
mid = lo + (hi - lo) / 2;
h1 = hash[a.fi + frst].get_hash(a.se, a.se + mid - 1), h2 = hash[b.fi + frst].get_hash(b.se, b.se + mid - 1);
if(h1 == h2){
lo = mid + 1;
ret = mid;
}
else{
hi = mid - 1;
}
}
assert(ret < m);
return (word[a.fi + frst][a.se + ret] > word[b.fi + frst][b.se + ret]);
};
pii best = {0, 0};
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(is_better(best, {i, j})){
best = {i, j};
}
}
}
for(int i = best.fi; i < best.fi + n; i++){
for(int j = best.se; j < best.se + m; j++){
cout << word[i][j];
}
cout << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
22 ms |
10332 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
19 ms |
10300 KB |
Output is correct |
12 |
Correct |
24 ms |
9252 KB |
Output is correct |
13 |
Correct |
20 ms |
10584 KB |
Output is correct |
14 |
Correct |
23 ms |
10564 KB |
Output is correct |
15 |
Correct |
19 ms |
10584 KB |
Output is correct |
16 |
Correct |
22 ms |
10580 KB |
Output is correct |
17 |
Correct |
24 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
444 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
22 ms |
10332 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
19 ms |
10300 KB |
Output is correct |
12 |
Correct |
24 ms |
9252 KB |
Output is correct |
13 |
Correct |
20 ms |
10584 KB |
Output is correct |
14 |
Correct |
23 ms |
10564 KB |
Output is correct |
15 |
Correct |
19 ms |
10584 KB |
Output is correct |
16 |
Correct |
22 ms |
10580 KB |
Output is correct |
17 |
Correct |
24 ms |
10588 KB |
Output is correct |
18 |
Correct |
283 ms |
101968 KB |
Output is correct |
19 |
Correct |
3 ms |
1624 KB |
Output is correct |
20 |
Correct |
4 ms |
2396 KB |
Output is correct |
21 |
Correct |
279 ms |
102268 KB |
Output is correct |
22 |
Correct |
292 ms |
102224 KB |
Output is correct |
23 |
Correct |
263 ms |
102224 KB |
Output is correct |
24 |
Correct |
327 ms |
102276 KB |
Output is correct |
25 |
Correct |
270 ms |
102188 KB |
Output is correct |
26 |
Correct |
314 ms |
102252 KB |
Output is correct |
27 |
Correct |
312 ms |
102220 KB |
Output is correct |