"Topic link"
DP[I][A1][A2][B1][B2] said that the first I food, the last two times of food in the mine were A1, A2,B1, B2 the same, the maximum number of coal obtained.
/* Think Thank Thunk */#include <cstdio> #include <cstring> #include <algorithm> #define REC (i, x, y) F
or (int i = x; i <= y; i++) using namespace std;
int n, dp[2][4][4][4][4];
inline int cread () {Char ch = getchar ();
for (; ch! = ' M ' && ch! = ' F ' && ch! = ' B '; ch = getchar ());
if (ch = = ' M ') return 1;
if (ch = = ' F ') return 2;
if (ch = = ' B ') return 3;
} inline int calc (int A, int b, int c) {int res = 1;
if (A! = 0 && A! = b && A! = c) res++;
if (b! = 0 && B! = c) res++;
return res;
} int main () {scanf ("%d", &n);
Memset (DP,-1, sizeof (DP));
Dp[0][0][0][0][0] = 0;
Rec (i, 0, n-1) {int t = Cread ();
Rec (A1, 0, 3) rec (A2, 0, 3) Rec (b1, 0, 3) rec (b2, 0, 3) {if (!~dp[i & 1][A1][A2][B1][B2]) continue;
Dp[~i & 1][a2][t][b1][b2] = Max (Dp[~i & 1][a2][t][b1][b2], Dp[i & 1][A1][A2][B1][B2] + calc (a1, A2, T)); Dp[~i & 1][a1][a2][b2][t] = Max (Dp[~i & 1][a1][a2][b2][t], Dp[i & 1][A1][A2][B1][B2] + calc (b1, B2, T));
}} int ans = 0;
Rec (A1, 0, 3) rec (A2, 0, 3) Rec (b1, 0, 3) rec (b2, 0, 3) ans = max (ans, dp[n & 1][a1][a2][b1][b2]);
printf ("%d\n", ans);
return 0;
}