CodeForces 603A_Alternative Thinking (DP)

Description

Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.

However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.

Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.

Input

The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).

The following line contains a binary string of length n representing Kevin's results on the USAICO.

Output

Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.

这道题大意是要从二进制串中找出最长的01依次交替的最长子串,不过在此之前可以对任一连续子串进行反转。

因为反转是连续的所以每个点的状态有6种,没有进行翻转,进行翻转,以及没有翻转且不能翻转。以及他们转与不转最终导致的本身值的状态是0还是1

可以只设置一个2*2*3的滚动数组便可以实现了,但是我嫌麻烦直接设了n的大小

#include<iostream>
using namespace std;
int n;
const int MAXN = 1e5 + 10;
char bin[MAXN];
int dp[MAXN][2][3];//0未1翻2远离
int lst; int maxLen;
int nowLen;
int Max(int a, int b){ return a > b ? a : b; }
int main()
{
	while (cin >> n){
		cin >> bin;
		if (bin[0] == '0'){
			dp[0][0][0] = 1;
			dp[0][1][0] = 0;
			dp[0][0][1] = -1;
			dp[0][1][1] = 1;
			dp[0][0][2] = -1;
			dp[0][1][2] = -1;
			lst = '0';
		}
		else{
			dp[0][0][0] = 0;
			dp[0][1][0] = 1;
			dp[0][0][1] = 1;
			dp[0][1][1] = -1;
			dp[0][0][2] = -1;
			dp[0][1][2] = -1;
			lst = '1';
		}
		maxLen = nowLen = 1;
		for (int i = 1; i < n; ++i){
			if (bin[i] == lst){
				++nowLen;
				maxLen = Max(nowLen, maxLen);
			}
			else{
				nowLen = 1;
			}
			lst = bin[i];
			if (bin[i] == '0'){
				dp[i][0][0] = Max(dp[i - 1][0][0], dp[i - 1][1][0] + 1);
				dp[i][0][1] = -1;
				dp[i][0][2] = Max(Max(dp[i - 1][0][1], dp[i - 1][1][1] + 1), Max(dp[i - 1][0][2], dp[i - 1][1][2] + 1));
				dp[i][1][0] = -1;
				dp[i][1][1] = Max(Max(dp[i - 1][0][1] + 1, dp[i - 1][1][1]), Max(dp[i - 1][0][0] + 1, dp[i - 1][1][0]));
				dp[i][1][2] = -1;
			}
			else{
				dp[i][0][0] = -1;
				dp[i][0][1] = Max(Max(dp[i - 1][0][1], dp[i - 1][1][1] + 1), Max(dp[i - 1][0][0], dp[i - 1][1][0] + 1));
				dp[i][0][2] = -1;
				dp[i][1][0] = Max(dp[i - 1][0][0] + 1, dp[i - 1][1][0]);
				dp[i][1][1] = -1;
				dp[i][1][2] = Max(Max(dp[i - 1][0][1] + 1, dp[i - 1][1][1]), Max(dp[i - 1][0][2] + 1, dp[i - 1][1][2]));
			}
		}
		int ans = Max(Max(dp[n - 1][0][0], dp[n - 1][0][1]), Max(Max(dp[n - 1][1][0], dp[n - 1][1][1]), Max(dp[n - 1][0][2], dp[n - 1][1][2])));
		
		cout << ans << endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值