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;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 1e5 + 10;
int dp[4][4][4][4];
int prev_dp[4][4][4][4];
void solve()
{
forn(a,4)forn(b,4)forn(c,4)forn(d,4)dp[a][b][c][d] = prev_dp[a][b][c][d] = -100000;
int n;
string s;
cin >> n >> s;
prev_dp[0][0][0][0] = 0;
for(int i = 1;i <= n;++i){
int x;
if(s[i - 1] == 'M')x = 1;
else if(s[i - 1] == 'B')x = 2;
else x = 3;
for(int a = 0;a < 4;++a){
for(int b = 0;b < 4;++b){
for(int c = 0;c < 4;++c){
for(int d = 0;d < 4;++d){
int add = 1;
if((x != a && a) && (x != b && b) && (a != b && a && b))add = 3;
else if((x != a && a) || (x != b && b) || (a != b && a && b))add = 2;
dp[x][a][c][d] = max(dp[x][a][c][d], prev_dp[a][b][c][d] + add);
add = 1;
if((x != c && c) && (x != d && d) && (c != d && c && d))add = 3;
else if((x != c && c) || (x != d && d) || (c != d && c && d))add = 2;
dp[a][b][x][c] = max(dp[a][b][x][c], prev_dp[a][b][c][d] + add);
}
}
}
}
for(int a = 0;a < 4;++a){
for(int b = 0;b < 4;++b){
for(int c = 0;c < 4;++c){
for(int d = 0;d < 4;++d){
prev_dp[a][b][c][d] = dp[a][b][c][d];
dp[a][b][c][d] = -100000;
}
}
}
}
}
int ans = 0;
for(int a = 0;a < 4;++a){
for(int b = 0;b < 4;++b){
for(int c = 0;c < 4;++c){
for(int d = 0;d < 4;++d){
ans = max(ans, prev_dp[a][b][c][d]);
}
}
}
}
cout << ans << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
# | 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... |