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>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;
const int N = 1e5 + 10;
ll dp[N][4][4][4][4];
int f[N];
void solve() {
int n; cin >> n;
string s; cin >> s;
for (int i = 1; i <= n; ++i) {
if (s[i-1] == 'M') f[i] = 1;
if (s[i-1] == 'F') f[i] = 2;
if (s[i-1] == 'B') f[i] = 3;
}
memset(dp, -1, sizeof(dp));
dp[0][0][0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
for (int s1 = 0; s1 < 4; ++s1) {
for (int s2 = 0; s2 < 4; ++s2) {
for (int s3 = 0; s3 < 4; ++s3) {
for (int s4 = 0; s4 < 4; ++s4) {
if (dp[i-1][s1][s2][s3][s4] == -1) continue;
set<int> ss;
if (s1 != 0) ss.insert(s1);
if (s2 != 0) ss.insert(s2);
ss.insert(f[i]);
int l = ss.size();
dp[i][s2][f[i]][s3][s4] = max(dp[i][s2][f[i]][s3][s4], dp[i-1][s1][s2][s3][s4] + l);
ss.clear();
if (s3 != 0) ss.insert(s3);
if (s4 != 0) ss.insert(s4);
ss.insert(f[i]);
l = ss.size();
dp[i][s1][s2][s4][f[i]] = max(dp[i][s1][s2][s4][f[i]], dp[i-1][s1][s2][s3][s4] + l);
}
}
}
}
}
ll ans = 0;
for (int s1 = 0; s1 < 4; ++s1) {
for (int s2 = 0; s2 < 4; ++s2) {
for (int s3 = 0; s3 < 4; ++s3) {
for (int s4 = 0; s4 < 4; ++s4) {
ans = max(ans, dp[n][s1][s2][s3][s4]);
}
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
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... |