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 endl "\n"
const int MOD = 998244353;
void solve(){
int n; cin >> n;
string s; cin >> s;
int dp[4][4][4][4];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int q = 0; q < 4; q++){
for(int l = 0; l < 4; l++){
dp[i][j][q][l] = -1;
}
}
}
}
dp[0][0][0][0] = 0;
for(int i = 0; i < n; i++){
int nw[4][4][4][4];
for(int k = 0; k < 4; k++){
for(int j = 0; j < 4; j++){
for(int q = 0; q < 4; q++){
for(int l = 0; l < 4; l++){
nw[k][j][q][l] = -1;
}
}
}
}
for(int k = 0; k < 4; k++){
for(int j = 0; j < 4; j++){
for(int q = 0; q < 4; q++){
for(int l = 0; l < 4; l++){
if(dp[k][j][q][l] == -1) continue;
pair<pair<int, int>, pair<int, int>> p = {{k, j}, {q, l}};
int c;
if(s[i] == 'M') c = 1;
else if(s[i] == 'F') c = 2;
else c = 3;
set<int> add;
add.insert(c); add.insert(p.first.first); add.insert(p.first.second); add.erase(0);
nw[p.first.second][c][p.second.first][p.second.second] = max(nw[p.first.second][c][p.second.first][p.second.second], dp[k][j][q][l] + (int)add.size());
add.clear();
add.insert(c); add.insert(p.second.first); add.insert(p.second.second); add.erase(0);
nw[p.first.first][p.first.second][p.second.second][c] = max(nw[p.first.first][p.first.second][p.second.second][c], dp[k][j][q][l] + (int)add.size());
}
}
}
}
for(int k = 0; k < 4; k++){
for(int j = 0; j < 4; j++){
for(int q = 0; q < 4; q++){
for(int l = 0; l < 4; l++){
dp[k][j][q][l] = nw[k][j][q][l];
}
}
}
}
}
int ans = 0;
for(int k = 0; k < 4; k++){
for(int j = 0; j < 4; j++){
for(int q = 0; q < 4; q++){
for(int l = 0; l < 4; l++){
ans = max(ans, dp[k][j][q][l]);
}
}
}
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("inpt.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//int tt; cin >> tt;
int tt = 1;
for(int t = 1; t <= tt; t++){
//cout << "Case #"<<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... |