#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::map<std::pair<int, int>, int> was;
int ans = 0;
was[{0, 0}] = 1;
for(int i = 1; i <= n; i++) {
std::pair<int, int> now = {pref[i][0] - pref[i][1], pref[i][0] - pref[i][2]};
if(was[now]) {
ans = std::max(ans, i - was[now] + 1);
}
else {
was[now] = i + 1;
}
}
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... |