首页 > 代码库 > [luoguP1136] 迎接仪式(DP)

[luoguP1136] 迎接仪式(DP)

传送门

 

每个字母只有两种选择,变成另一个或者不变。

所以f[i][j][k]表示前i个字母有j个j变成z,有k个z变成j

只需要比较j==k时的答案就行

#include <cstdio>#include <cstring>#define N 505#define max(x, y) ((x) > (y) ? (x) : (y))int n, m, ans;char s[N];int f[N][105][105];int main(){	int i, j, k;	scanf("%d %d", &n, &m);	scanf("%s", s + 1);	memset(f, -0x3f, sizeof(f));	f[0][0][0] = f[1][0][0] = 0;	s[1] == ‘j‘ ? f[1][1][0] = 0 : f[1][0][1] = 0;	for(i = 2; i <= n; i++)		for(j = 0; j <= m; j++)			for(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(s[i - 1] == ‘j‘ && s[i] == ‘j‘ && j) f[i][j][k] = max(f[i][j][k], f[i - 2][j - 1][k] + 1);				if(s[i - 1] == ‘z‘ && s[i] == ‘z‘ && k) f[i][j][k] = max(f[i][j][k], f[i - 2][j][k - 1] + 1);				if(s[i - 1] == ‘z‘ && s[i] == ‘j‘ && j && k) 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;}

  

[luoguP1136] 迎接仪式(DP)