#include <bits/stdc++.h>
#define int int64_t
signed main() {
int n;
std::cin >> n;
std::vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
char x;
std::cin >> x;
if(x == 'J') {
a[i] = 0;
}
else if(x == 'O') {
a[i] = 1;
}
else {
a[i] = 2;
}
}
std::vector<std::vector<int>> pref(n + 2, std::vector<int>(4, 0));
for(int i = 1; i <= n; i++) {
for(int j = 0; j < 3; j++) {
pref[i][j] = pref[i - 1][j];
}
pref[i][a[i]] += 1;
}
std::vector<std::vector<int>> next(n + 2, std::vector<int>(n + 2, n + 1));
for(int i = n; i >= 1; i--) {
for(int j = 0; j < 3; j++) {
next[i][j] = next[i + 1][j];
}
next[i][a[i]] = i;
}
int ans = 0;
for(int i = 1; i <= n; i++) {
int now = 0;
int cnt = pref[n][0] - pref[i - 1][0];
for(int j = 1; j < 3; j++) {
int New = pref[n][j] - pref[i - 1][j];
if(New < cnt) {
cnt = New;
now = j;
}
}
int j = next[i][now];
while(cnt-- && j <= n) {
if(pref[j][0] - pref[i - 1][0] == pref[j][1] - pref[i - 1][1] && pref[j][0] - pref[i - 1][0] == pref[j][2] - pref[i - 1][2]) {
ans = std::max(ans, j - i + 1);
}
j = next[j + 1][now];
}
}
std::cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |