#include "bits/stdc++.h"
using namespace std;
#define SZ(a) (int)(a).size()
#define ALL(a) (a).begin(),(a).end()
signed main() {
int n;
cin >> n;
vector<vector<int>> pos(26);
for (int i = 1; i <= n; ++i) {
char c; cin >> c;
pos[int(c - 'a')].push_back(i);
}
int ans = 0;
for (int x = 0; x < 26; ++x) {
for (int y = 0; y < 26; ++y) {
if (x == y) {
continue;
}
if (!SZ(pos[x]) || !SZ(pos[y])) {
continue;
}
vector<int> a = {0};
{
int j = 0;
for (int i : pos[x]) {
while (j < SZ(pos[y]) && pos[y][j] < i) {
a.push_back(-1);
j++;
}
a.push_back(1);
}
while (j < SZ(pos[y])) {
a.push_back(-1);
j++;
}
}
int sz = SZ(a);
int mn = n + 1, lst = 0;
vector<int> pref(sz, 0);
for (int i = 1; i < sz; ++i) {
pref[i] = pref[i - 1] + a[i];
if (i > 1 && a[i] != a[i - 1]) {
for (int j = lst; j < i - 1; ++j) {
mn = min(mn, pref[j]);
}
lst = i - 1;
}
ans = max(ans, pref[i] - mn);
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
536 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
1092 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
689 ms |
7476 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
330 ms |
5528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
601 ms |
6960 KB |
Output is correct |
2 |
Correct |
491 ms |
5396 KB |
Output is correct |
3 |
Correct |
170 ms |
6312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
752 ms |
7284 KB |
Output is correct |
2 |
Correct |
93 ms |
12272 KB |
Output is correct |
3 |
Correct |
137 ms |
7316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
766 ms |
7360 KB |
Output is correct |
2 |
Correct |
63 ms |
13360 KB |
Output is correct |
3 |
Correct |
221 ms |
7120 KB |
Output is correct |