#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 2e5+5;
const long long mpI = 1000000007, mpO = 100000009, mpJ = -(mpI+mpO);
int n, ans = 0;
string s;
long long pref[N], a[N];
int cnt[30];
map<long long,int> check;
int main()
{
cin >> n >> s;
if (n <= 4000)
{
for (int i = 0; i < n; i++)
{
memset(cnt,0,sizeof cnt);
for (int j = i; j < n; j++)
{
cnt[s[j]-'A']++;
if (cnt['I'-'A'] == cnt['O'-'A'] && cnt['I'-'A'] == cnt['J'-'A'])
ans = max(ans,j-i+1);
}
}
return cout << ans, 0;
}
for (int i = 0; i < n; i++)
{
if (s[i] == 'I') a[i+1] = mpI;
else if (s[i] == 'O') a[i+1] = mpO;
else a[i+1] = mpJ;
}
for (int i = 1; i <= n; i++)
{
pref[i] = pref[i-1] + a[i];
if (check[pref[i]])
{
ans = max(ans,i-check[pref[i]]);
}
else
check[pref[i]] = i;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
380 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 |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
380 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
252 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 |
4 ms |
376 KB |
Output is correct |
2 |
Correct |
11 ms |
376 KB |
Output is correct |
3 |
Correct |
22 ms |
296 KB |
Output is correct |
4 |
Correct |
38 ms |
412 KB |
Output is correct |
5 |
Correct |
37 ms |
256 KB |
Output is correct |
6 |
Correct |
26 ms |
376 KB |
Output is correct |
7 |
Correct |
55 ms |
256 KB |
Output is correct |
8 |
Correct |
31 ms |
376 KB |
Output is correct |
9 |
Correct |
35 ms |
376 KB |
Output is correct |
10 |
Correct |
73 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
760 KB |
Output is correct |
2 |
Correct |
10 ms |
1400 KB |
Output is correct |
3 |
Correct |
15 ms |
2168 KB |
Output is correct |
4 |
Correct |
29 ms |
3704 KB |
Output is correct |
5 |
Correct |
47 ms |
5756 KB |
Output is correct |
6 |
Correct |
57 ms |
6540 KB |
Output is correct |
7 |
Correct |
62 ms |
7072 KB |
Output is correct |
8 |
Correct |
59 ms |
6412 KB |
Output is correct |
9 |
Correct |
58 ms |
6716 KB |
Output is correct |
10 |
Correct |
55 ms |
6540 KB |
Output is correct |
11 |
Correct |
45 ms |
7816 KB |
Output is correct |
12 |
Correct |
35 ms |
5128 KB |
Output is correct |
13 |
Correct |
33 ms |
4460 KB |
Output is correct |
14 |
Correct |
56 ms |
8584 KB |
Output is correct |
15 |
Correct |
31 ms |
4232 KB |
Output is correct |