【Codeforces Round 334 (Div 2)C】【脑洞】Alternative Thinking 最多反转一个区间使得最长跳跃子串的长度尽可能长

来源:互联网 发布:gltools优化王者 编辑:程序博客网 时间:2024/06/08 12:38
Alternative Thinking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 anot-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.

Sample test(s)
input
810000011
output
5
input
201
output
2
Note

In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.

In the second sample, Kevin can flip the entire string and still have the same score.

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;int n;char s[N];int main(){while(~scanf("%d",&n)){scanf("%s",s+1);int len=0;for(int i=1;i<=n;++i)if(s[i]!=s[i-1])++len;printf("%d\n",min(len+2,n));}return 0;}/*【trick&&吐槽】一些设计思维构想的题目,如果数据比较好构造,我们一定要充分构造数据,以确保算法的正确性。【题意】给你一个01串,我们可以改变一个区间内的01关系(0变1,1变0),问你这个01串中最长跳跃子串的长度是多少。所谓跳跃子串,就是指,一个串,相邻位置的字符都不相同。子串不要求连续。【类型】脑洞【分析】这题的做法其实很简单,然而想起来并不一定想得到,而是可能陷入比较麻烦的做法。最好的实现方法是什么呢?我们直接遍历这个序列。1,求出多少次出现a[i]!=a[i-1],这个波动的次数+1,就是不经过改变情况下的跳跃子串的最大长度。2,求出多少个位置a[i]==a[i-1],这个位置的个数,如果是1,我们可以改变之前或者之后的所有数,之前答案不变,答案增值为1。如果是2,我们可以改变范围内的所有数,之前答案不变,答案增加2.实际上,只要两行代码——int len;for(int i=1;i<=n;++i)if(s[i]!=s[i-1])++len;printf("%d\n",min(len+2,n));这道题可以这样很简单地就AC啦!【时间复杂度&&优化】O(n)*/


0 0
原创粉丝点击