#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
int a[N];
int f[N], g[N];
vector<int> idx[26];
int ans = 0;
int b[N];
int ptr;
void solve(int mn, int mx) {
ptr = 1;
int i = 0, j = 0;
while (i < (int) idx[mn].size() && j < (int) idx[mx].size()) {
if (idx[mn][i] < idx[mx][j]) {
b[ptr] = -1;
ptr++;
i++;
} else if (idx[mx][j] < idx[mn][i]) {
b[ptr] = 1;
ptr++;
j++;
}
}
while (i < (int) idx[mn].size()) {
b[ptr] = -1;
ptr++;
i++;
}
while (j < (int) idx[mx].size()) {
b[ptr] = 1;
ptr++;
j++;
}
int sum = 0;
for (int i = 1; i <= ptr; i++) {
sum = max(b[i], b[i] + sum);
f[i] = sum;
}
sum = 0;
for (int i = ptr; i >= 1; i--) {
sum = max(b[i], b[i] + sum);
g[i] = sum;
}
int res = 0;
for (int i = 1; i <= ptr; i++) {
if (b[i] == -1) {
res = max(res, f[i] + g[i] + 1);
}
}
ans = max(ans, res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
char d;
cin >> d;
a[i] = (d - 'a');
idx[a[i]].push_back(i);
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
if (i != j) {
solve(i, j);
}
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
7224 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
5 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
626 ms |
18008 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Incorrect |
373 ms |
17692 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
627 ms |
17820 KB |
Output is correct |
2 |
Incorrect |
472 ms |
15688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
625 ms |
18076 KB |
Output is correct |
2 |
Incorrect |
231 ms |
21156 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
625 ms |
17816 KB |
Output is correct |
2 |
Incorrect |
216 ms |
21028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |