#include<bits/stdc++.h>
using namespace std;
#define sz(v) (int)(v).size()
template<typename T> bool ckmx(T& a, const T& b){
if(a < b)
return a = b, true;
return false;
}
int n; string S;
namespace Sub1{
void process()
{
auto calc = [&](string x) -> int{
int answer = 0;
for(int i = 0; i < sz(x); i++){
if(i == 0){
answer = answer + 1;
}
else if(i == 1){
answer = answer + 1 + (x[i] != x[i - 1]);
}
else{
set<int> s = {x[i], x[i - 1], x[i - 2]};
answer = answer + sz(s);
}
}
return answer;
};
int answer = 0;
for(int mask = 0; mask < (1 << n); mask++){
string mine1 = "", mine2 = "";
for(int i = 0; i < n; i++){
if(mask >> i & 1){
mine1 += S[i];
}
else mine2 += S[i];
}
answer = max(answer, calc(mine1) + calc(mine2));
}
cout << answer << "\n";
}
}
/*
We only care about 2 previous type
dp[i][mine 1 first][mine 1 second][mine 2 first][mine 2 second]:
at i, 2 previous type of mine 1, 2 previous type of mine 2
if we did not choose, the type will be 0
*/
namespace Sub2{
int dp[2][4][4][4][4];
void process()
{
auto calc = [&](int first, int second, int cur) -> int{
set<int> s = {first, second, cur};
return sz(s) - (!((*s.begin())));
};
memset(dp[1], -0x3f, sizeof(dp[1]));
dp[1][0][0][0][0] = 0;
int answer = 0;
for(int i = 0; i < n; i++){
int type = (S[i] == 'M' ? 1 : (S[i] == 'F' ? 2 : 3));
int cur = (i & 1), pre = (i & 1) ^ 1;
memset(dp[cur], -0x3f, sizeof(dp[cur]));
for(int m1t1 = 0; m1t1 <= 3; m1t1++){
for(int m1t2 = 0; m1t2 <= 3; m1t2++){
for(int m2t1 = 0; m2t1 <= 3; m2t1++){
for(int m2t2 = 0; m2t2 <= 3; m2t2++){
ckmx(dp[cur][m1t2][type][m2t1][m2t2], dp[pre][m1t1][m1t2][m2t1][m2t2] + calc(m1t1, m1t2, type));
ckmx(dp[cur][m1t1][m1t2][m2t2][type], dp[pre][m1t1][m1t2][m2t1][m2t2] + calc(m2t1, m2t2, type));
ckmx(answer, dp[cur][m1t2][type][m2t1][m2t2]);
ckmx(answer, dp[cur][m1t1][m1t2][m2t2][type]);
}
}
}
}
}
cout << answer << "\n";
}
}
void solve()
{
cin >> n >> S;
Sub2::process();
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'int32_t main()':
miners.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
miners.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |