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;
int n;
string s; /// M-1, B-2, F-3
vector<vector<vector<vector<vector<vector<long long>>>>>>table;
long long rec(int indx, int mine, int last11, int last12, int last21, int last22){
vector<int>v(4);
v[0] = last11;
v[1] = last12;
v[2] = last21;
v[3] = last22;
long long res = 0;
set<int>st;
if(indx == n)return 0;
if(table[mine][last11][last12][last21][last22][indx] != -1e18)return table[mine][last11][last12][last21][last22][indx];
if(mine == 1){
if(last11 != 0)st.insert(last11);
if(last12 != 0)st.insert(last12);
last12 = last11;
if(s[indx] == 'M')last11 = 1;
if(s[indx] == 'B')last11 = 2;
if(s[indx] == 'F')last11 = 3;
}
else{
if(last21 != 0)st.insert(last21);
if(last22 != 0)st.insert(last22);
last22 = last21;
if(s[indx] == 'M')last21 = 1;
if(s[indx] == 'B')last21 = 2;
if(s[indx] == 'F')last21 = 3;
}
if(s[indx] == 'M')st.insert(1);
if(s[indx] == 'B')st.insert(2);
if(s[indx] == 'F')st.insert(3);
res = st.size() + max(rec(indx + 1, 0, last11, last12, last21, last22), rec(indx + 1, 1, last11, last12, last21, last22));
return table[mine][v[0]][v[1]][v[2]][v[3]][indx] = res;
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
cin >> n >> s;
table.assign(2, vector<vector<vector<vector<vector<long long>>>>>(4, vector<vector<vector<vector<long long>>>>(4, vector<vector<vector<long long>>>(4, vector<vector<long long>>(4, vector<long long>(n + 1, -1e18))))));
cout << max(rec(0, 0, 0, 0, 0, 0), rec(0, 1, 0, 0, 0, 0));
}
# | 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... |