This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
char cur1 = '?', prev1 = '?';
char cur2 = '?', prev2 = '?';
int ans = 0;
string A, B;
for (int i=0; i<n; i++) {
set<char> a, b;
a.insert(s[i]);
if (cur1 != '?') a.insert(cur1);
if (prev1 != '?') a.insert(prev1);
b.insert(s[i]);
if (cur2 != '?') b.insert(cur2);
if (prev2 != '?') b.insert(prev2);
ans = ans + max((int)a.size(),(int)b.size());
if ((int)a.size() > (int)b.size()) {
prev1 = cur1;
cur1 = s[i];
A.push_back(s[i]);
}
if ((int)a.size() < (int)b.size()) {
prev2 = cur2;
cur2 = s[i];
B.push_back(s[i]);
}
if ((int)a.size() == (int)b.size()) {
if ((cur1 == s[i] && cur2 == s[i]) || (cur1 != s[i] && cur2 != s[i])) {
if ((int)A.size() >= (int)B.size()) {
prev2 = cur2;
cur2 = s[i];
B.push_back(s[i]);
}
else {
prev1 = cur1;
cur1 = s[i];
A.push_back(s[i]);
}
}
else {
if (cur1 != s[i]) {
prev1 = cur1;
cur1 = s[i];
A.push_back(s[i]);
}
else {
prev2 = cur2;
cur2 = s[i];
B.push_back(s[i]);
}
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |