#include<iostream>
#include<string>
#include<map>
int main(){
std::ios::sync_with_stdio(0);std::cin.tie(0);
int n;std::cin>>n;
std::string s;s.reserve(n);std::cin>>s;
std::map<std::pair<int,int>,int> minpos;
minpos.insert({{0,0},-1});
std::pair<int,int> cur; // {J-I, O-I} count
int ans=0;
for(int i=0;i<n;++i){
switch(s[i]){
case 'J':++cur.first;break;
case 'O':++cur.second;break;
case 'I':--cur.first;--cur.second;break;
default:__builtin_unreachable();
}
auto iter=minpos.find(cur);
if(iter!=end(minpos))ans=std::max(ans,i-iter->second);
minpos.insert({cur,i});
}
std::cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
3 ms |
488 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
8 ms |
1016 KB |
Output is correct |
3 |
Correct |
13 ms |
1528 KB |
Output is correct |
4 |
Correct |
25 ms |
2172 KB |
Output is correct |
5 |
Correct |
36 ms |
3320 KB |
Output is correct |
6 |
Correct |
46 ms |
3520 KB |
Output is correct |
7 |
Correct |
47 ms |
4024 KB |
Output is correct |
8 |
Correct |
46 ms |
3320 KB |
Output is correct |
9 |
Correct |
46 ms |
3576 KB |
Output is correct |
10 |
Correct |
45 ms |
3448 KB |
Output is correct |
11 |
Correct |
38 ms |
5112 KB |
Output is correct |
12 |
Correct |
30 ms |
2168 KB |
Output is correct |
13 |
Correct |
22 ms |
1528 KB |
Output is correct |
14 |
Correct |
42 ms |
6136 KB |
Output is correct |
15 |
Correct |
20 ms |
1144 KB |
Output is correct |