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;
typedef long long ll;
const ll mod = 1e9 + 7;
#pragma GCC optimize("Ofast")
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define sc second
#define endl '\n'
#define gl ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, ans = 0, a = 0, b = 0;
string s, s1 = "", s2 = "";
int main()
{
cin >> n >> s;
map<char, int> m1, m2;
m1['M'] = 0;
m1['B'] = 0;
m1['F'] = 0;
m2['M'] = 0;
m2['B'] = 0;
m2['F'] = 0;
for(int i = 0; i < n; i++){
if(m1[s[i]] == 0){
m1[s[i]]++;
a += 1;
ans += a;
s1 += s[i];
if(s1.size() > 2){
m1[s1[s1.size() - 3]]--;
if(m1[s1[s1.size() - 3]] == 0) a--;
}
}
else if(m2[s[i]] == 0){
m2[s[i]]++;
b += 1;
ans += b;
s2 += s[i];
if(s2.size() > 2){
m2[s2[s2.size() - 3]]--;
if(m2[s2[s2.size() - 3]] == 0) b--;
}
}
else if(m1[s[i]] <= m2[s[i]]){
m1[s[i]]++;
ans += a;
s1 += s[i];
if(s1.size() > 2){
m1[s1[s1.size() - 3]]--;
if(m1[s1[s1.size() - 3]] == 0) a--;
}
}
else{
m2[s[i]]++;
ans += b;
s2 += s[i];
if(s2.size() > 2){
m2[s2[s2.size() - 3]]--;
if(m2[s2[s2.size() - 3]] == 0) b--;
}
}
}
cout << ans << endl;
}
# | 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... |