#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
const int N = 1e6 + 5;
char a[N];
vector<int>pos[30];
int n,val[N],maxPreSum[N],maxSufSum[N];
int calc(int a,int b) {
if (pos[a].size() == 0 || pos[b].size() == 0) return 0;
vector<pii>vec;
for (auto x: pos[a]) vec.push_back(mp(x,1));
for (auto x: pos[b]) vec.push_back(mp(x,-1));
int m = vec.size();
sort(vec.begin(),vec.end());
maxPreSum[0] = maxSufSum[m + 1] = 0;
int sum = 0;
for (int i = 1; i <= m; i++) {
sum = max(sum + vec[i - 1].se,vec[i - 1].se);
maxPreSum[i] = sum;
}
sum = 0;
for (int i = m; i > 0; i--) {
sum = max(sum + vec[i - 1].se,vec[i - 1].se);
maxSufSum[i] = sum;
}
int ans = 0;
for (int i = 1; i <= m; i++) {
if (vec[i].se == 1) continue;
int tmp = maxPreSum[i - 1] + maxSufSum[i + 1] - 1;
tmp = max(tmp,maxPreSum[i - 1] - 1);
tmp = max(tmp,maxSufSum[i - 1] - 1);
ans = max(ans,tmp);
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) pos[a[i] - 'a'].push_back(i);
int ans = 0;
for (int i = 0; i < 26; i++)
for (int j = 0; j < 26; j++)
if (i != j) ans = max(ans,calc(i,j));
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2512 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4596 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
4680 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
5212 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
20 ms |
4720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1044 ms |
14068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1028 ms |
14132 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1018 ms |
14380 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1055 ms |
14132 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |