This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,int> M;
ll N,J,O,I,cnt;
int ans;
string S;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> N >> S;
M.insert(make_pair(0,0));
for(int i=0; i<N; i++){
if(S[i]=='J') O--,I--;
if(S[i]=='O') O++;
if(S[i]=='I') I++;
cnt = O*(N+1)+I;
if(M.find(cnt)==M.end()) M.insert(make_pair(cnt,i+1));
}
for(int i=N-1; i>=0; i--){
cnt = O*(N+1)+I;
if(M.find(cnt)!=M.end()) ans = max(ans,i+1-M[cnt]);
if(S[i]=='J') O++,I++;
if(S[i]=='O') O--;
if(S[i]=='I') I--;
}
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... |