迎接仪式
【代码】
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 510;
int f[N][120][120], ans = 0;
char s[N];
int main() {
int n,m;
scanf("%d%d%s", &n, &m, s + 1);
memset(f, 0 , sizeof f);
f[0][0][0] = f[1][0][0] = f[1][s[1] == 'j'][s[1] == 'z'] = 0;
for(int i = 2; i <= n; ++i)
for(int j = 0 ; j <= m; ++j)
for(int k = 0; k <= m; ++k) {
f[i][j][k] = f[i - 1][j][k];
if(s[i - 1] == 'j' && s[i] == 'z') f[i][j][k] = max(f[i][j][k], f[i - 2][j][k] + 1);
if(k&&s[i - 1] == 'z' && s[i] == 'z') f[i][j][k] = max(f[i][j][k], f[i - 2][j][k - 1] + 1);
if(j&&s[i - 1] == 'j' && s[i] == 'j') f[i][j][k] = max(f[i][j][k], f[i - 2][j - 1][k] + 1);
if(j&&k&&s[i - 1] == 'z' && s[i] == 'j') f[i][j][k] = max(f[i][j][k], f[i - 2][j - 1][k - 1] + 1);
if(j == k)ans = max(ans, f[i][j][k]);
}
printf("%d\n",ans);
return 0;
}