// yukicoder: No.291 黒い文字列 // 2019.6.21 bal4u #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif char f[128]; int x; int ins(char *s) // 文字列の入力 スペース以下の文字で入力終了 { int c; char *p = s; x = 0; while ((c = gc()) > ' ') { if (c == '?') x++; if (f[c]) *s++ = c; } *s = 0; return s-p; } int kuroi[6][100], sz[5]; char s[105]; char dp[105][22][22][22][22]; int main() { int i, j, t, w, k, u, r, o, ans; f['K'] = 1, f['U'] = 2, f['R'] = 3, f['O'] = 4, f['I'] = 5, f['?'] = 0x1f; w = ins(s+1); if (x == 0) { sz[0] = 101; for (i = 1; i <= w; i++) { j = f[s[i]]; if (sz[j] < sz[j-1] && kuroi[j-1][sz[j]] < i) kuroi[j][sz[j]++] = i; } ans = sz[5]; } else if (x == w) ans = x / 5; else { dp[0][0][0][0][0] = 1, ans = 1; for (i = 1; i <= w; i++) { if ((j = f[s[i]]) <= 5) j = 1 << (j-1); for (k = 0; k <= 20; k++) for (u = 0; u <= 20; u++) for (r = 0; r <= 20; r++) for (o = 0; o <= 20; o++) if (t = dp[i-1][k][u][r][o]) { if (j & 1) { if (k <20) { if (dp[i][k+1][u][r][o] < t) dp[i][k+1][u][r][o] = t; } else { if (dp[i][k ][u][r][o] < t) dp[i][k ][u][r][o] = t; } } if (j & 2) { if (u < k) { if (dp[i][k][u+1][r][o] < t) dp[i][k][u+1][r][o] = t; } else { if (dp[i][k][u ][r][o] < t) dp[i][k][u ][r][o] = t; } } if (j & 4) { if (r < u) { if (dp[i][k][u][r+1][o] < t) dp[i][k][u][r+1][o] = t; } else { if (dp[i][k][u][r ][o] < t) dp[i][k][u][r ][o] = t; } } if (j & 8) { if (o < r) { if (dp[i][k][u][r][o+1] < t) dp[i][k][u][r][o+1] = t; } else { if (dp[i][k][u][r][o ] < t) dp[i][k][u][r][o ] = t; } } if (j & 16) { if (t <= o) { if (dp[i][k][u][r][o] < t+1) { dp[i][k][u][r][o] = t+1; if (t+1 > ans) ans = t + 1; } } else if (dp[i][k][u][r][o] < t) dp[i][k][u][r][o] = t; } } } ans--; } printf("%d\n", ans); return 0; }