#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
#define cnt_I first
#define cnt_O second.first
#define cnt_J second.second
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int N = 2e5+5;
int n, ans = 0;
string s;
iii pref[N];
map<iii,int> check;
int main()
{
cin >> n >> s;
if (s[0] == 'I') pref[0].cnt_I++;
else if (s[0] == 'O') pref[0].cnt_O++;
else pref[0].cnt_J++;
check[pref[0]] = 1;
for (int i = 1; i < n; i++)
{
pref[i] = pref[i-1];
if (s[i] == 'I') pref[i].cnt_I++;
else if (s[i] == 'O') pref[i].cnt_O++;
else pref[i].cnt_J++;
int tmp = min(pref[i].cnt_I,min(pref[i].cnt_O,pref[i].cnt_J));
pref[i].cnt_I -= tmp;
pref[i].cnt_O -= tmp;
pref[i].cnt_J -= tmp;
if (!pref[i].cnt_I && !pref[i].cnt_O && !pref[i].cnt_J)
ans = max(ans,i+1);
else if (check[pref[i]])
{
ans = max(ans,i+1-check[pref[i]]);
}
else
check[pref[i]] = i+1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
268 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 |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
3 ms |
504 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 |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
12 ms |
1272 KB |
Output is correct |
3 |
Correct |
18 ms |
2040 KB |
Output is correct |
4 |
Correct |
34 ms |
3376 KB |
Output is correct |
5 |
Correct |
49 ms |
5128 KB |
Output is correct |
6 |
Correct |
63 ms |
5768 KB |
Output is correct |
7 |
Correct |
67 ms |
6408 KB |
Output is correct |
8 |
Correct |
63 ms |
5736 KB |
Output is correct |
9 |
Correct |
64 ms |
5896 KB |
Output is correct |
10 |
Correct |
63 ms |
5920 KB |
Output is correct |
11 |
Correct |
49 ms |
7048 KB |
Output is correct |
12 |
Correct |
44 ms |
4360 KB |
Output is correct |
13 |
Correct |
41 ms |
3720 KB |
Output is correct |
14 |
Correct |
48 ms |
8072 KB |
Output is correct |
15 |
Correct |
34 ms |
3464 KB |
Output is correct |