#include <bits/stdc++.h>
using namespace std;
const int M = 26;
int n;
string s;
int best[M][M];
int invalid[M][M];
int update_local[M];
int update_global[M];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>s;
int ans = 0;
for (int _ = 0; _ < 2; _++) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++)
best[i][j] = invalid[i][j] = -1e9;
update_local[i] = update_global[i] = 0;
}
for (int i = 0; i < n; i++) {
// [i][s[i]-'a']--
update_global[s[i]-'a']--;
// [s[i]-'a'][i]++
update_local[s[i]-'a']++;
for (int c = 0; c < M; c++) if (c != s[i]-'a') {
// make things valid
best[c][s[i]-'a'] = max(best[c][s[i]-'a'], invalid[c][s[i]-'a']);
best[c][s[i]-'a'] = max(best[c][s[i]-'a'], -1-update_local[c]-update_global[s[i]-'a']);
ans = max(ans, best[s[i]-'a'][c] + update_local[s[i]-'a'] + update_global[c]);
// now new updates are invalid until activated
invalid[s[i]-'a'][c] = max(invalid[s[i]-'a'][c], 1 - update_local[s[i]-'a'] - update_global[c]);
}
}
cout << endl;
reverse(begin(s), end(s));
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
700 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
17 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
746 ms |
2448 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
609 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
746 ms |
2476 KB |
Output is correct |
2 |
Correct |
599 ms |
2188 KB |
Output is correct |
3 |
Correct |
634 ms |
2196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
747 ms |
2548 KB |
Output is correct |
2 |
Correct |
746 ms |
2580 KB |
Output is correct |
3 |
Correct |
731 ms |
2448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
745 ms |
2472 KB |
Output is correct |
2 |
Correct |
739 ms |
2520 KB |
Output is correct |
3 |
Correct |
733 ms |
2476 KB |
Output is correct |