外面oj内存限制128MB
查看原帖
外面oj内存限制128MB
525817
llqqhh楼主2023/4/8 21:16

rt, 本地一点问题没有,提交就re

#include<bits/stdc++.h>
using namespace std;

int n, f[100010][5][5][5][5];//到i为止 1的最后两个,2的最后两个
char s[100010];

int ttt(char c)
{
	if(c == 'M') return 1;
	if(c == 'F') return 2;
	if(c == 'B') return 3;
	return 0;
}

int solve(int a, int b, int c)
{
	if(a == 0)
	{
		if(b == 0) return 1;
		if(b == c) return 1;
		return 2;
	}
	if(a == b && b == c) return 1;
	if(a != b && b != c && a != c) return 3;
	return 2;
}

int main()
{
	cin >> n;
	scanf("%s", s+1);
	memset(f, -1, sizeof(f));
	f[0][0][0][0][0] = 0;
	for(int i = 1; i <= n; i ++)
	{
		for(int p = 0; p <= 3; p ++)
			for(int q = 0; q <= 3; q ++)
				for(int h = 0; h <= 3; h ++)
					for(int t = 0; t <= 3; t ++)
						if(f[i-1][p][q][h][t] != -1)
						{
							f[i][q][ttt(s[i])][h][t] = max(f[i][q][ttt(s[i])][h][t], f[i-1][p][q][h][t] + solve(p, q, ttt(s[i])));
							f[i][p][q][t][ttt(s[i])] = max(f[i][p][q][t][ttt(s[i])], f[i-1][p][q][h][t] + solve(h, t, ttt(s[i])));
						}
	}
	int ans = 0;
	for(int p = 0; p <= 3; p ++)
		for(int q = 0; q <= 3; q ++)
			for(int h = 0; h <= 3; h ++)
				for(int t = 0; t <= 3; t ++)
					ans = max(ans, f[n][p][q][h][t]);
	cout << ans << endl;
	return 0;
}
2023/4/8 21:16
加载中...