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.

Example

Input
8
10000011
Output
5
Input
2
01
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.

懂了意思就好了 ,只允许该连续的子串,不可以跳着改,如果有三个连续的,原来的数量加2,比如10001,原来是3,改了之后为10101,变成5,如果有两个连续的,加1,比如1101,原来是3,改了之后0101,变为4,加入有间隔的两个及以上这种模式,那么原数量加2,如11011,原来是3,改了之后10101,变成5,下划线代表子串被改变。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std; int main()
{
int n,c=,er=,san=,min=;
char str[]="";
scanf("%d",&n);
getchar();
for(int i=;i<=n+;i++)
{
if(i<n+)scanf("%c",&str[i]);
if(str[i]!=str[i-])
{
if(c==)er++;
else if(c>=)san++;
if(i<n+)min++;
c=;
}
else c++;
}
//cout<<er<<' '<<san<<endl;
if(san||er>)min+=;
else if(er) min+=;
printf("%d",min);
}

Alternative Thinking 找规律的更多相关文章

  1. hdu 3951 - Coin Game(找规律)

    这道题是有规律的博弈题目,,, 所以我们只需要找出规律来就ok了 牛人用sg函数暴力找规律,菜鸟手工模拟以求规律...[牢骚] if(m>=2) { if(n<=m) {first第一口就 ...

  2. HDU 5703 Desert 水题 找规律

    已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这 ...

  3. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  4. CF456B Fedya and Maths 找规律

    http://codeforces.com/contest/456/problem/B CF#260 div2 B Fedya and Maths Codeforces Round #260 B. F ...

  5. hdu 4731 2013成都赛区网络赛 找规律

    题意:找字串中最长回文串的最小值的串 m=2的时候暴力打表找规律,打表可以用二进制枚举

  6. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  7. 找规律 ZOJ3498 Javabeans

    Javabeans are delicious. Javaman likes to eat javabeans very much. Javaman has n boxes of javabeans. ...

  8. C基础之递归(思想很重要,学会找规律)

    递归思想的条件:1.函数自己调用自己 2.函数必须有一个固定的返回值(如果没有这个条件会发生死循环) ----规律很重要 简单递归题目一: 设计一个函数计算一个整数的n次方,比如2的3次方,就是8 步 ...

  9. BZOJ-1228 E&D 博弈SG+找啊找啊找规律

    讨厌博弈,找规律找半天还是错的.... 1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MB Submit: 666 Solv ...

随机推荐

  1. Codeforces 859C - Pie Rules

    859C - Pie Rules 思路: dp 我们知道无论谁拿到decider token他拿不拿蛋糕都是确定的,都是使自己最优的结果. 于是 定义状态:dp[i]表示到第i个位置拿到decider ...

  2. js setInterval不能访问外网

    今天调用js setInterval,发现不能访问外网,或者说不能访问本身域名以外的其他域名..不知道什么原因,老是弹出: 网络延时,请稍后再试! setInterval(function(){ va ...

  3. 『TensorFlow』项目资源分享

    TF中文社区 TF_GOOGLE官方代码学习 1.TensorFlow-Slim TF-Slim 是 tensorflow 较新版本的扩充包,可以简化繁杂的网络定义,其中也提供了一些demo: Ale ...

  4. Codeforces Round #449 (Div. 1)C - Willem, Chtholly and Seniorious

    ODT(主要特征就是推平一段区间) 其实就是用set来维护三元组,因为数据随机所以可以证明复杂度不超过O(NlogN),其他的都是暴力维护 主要操作是split,把区间分成两个,用lowerbound ...

  5. python-day12--函数进阶

    1.命名空间: 分三种:全局命名空间,局部命名空间,内置命名空间. 加载顺序:内置命名空间→全局命名空间→局部命名空间 取值顺序:局部命名空间→全局命名空间→内置命名空间 2.作用域: 作用域就是作用 ...

  6. hdu1864(01包)

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. git在eclipse中的配置 转载

    git在eclipse中的配置 转载 一_安装EGIT插件 http://download.eclipse.org/egit/updates/ 或者使用Eclipse Marketplace,搜索EG ...

  8. .NET 性能优化方法总结==转

    .NET 性能优化方法总结 目录 目录 1. C#语言方面... 4 1.1 垃圾回收... 4 1.1.1 避免不必要的对象创建... 4 1.1.2 不要使用空析构函数 ★... 4 1.1.3 ...

  9. Idea热部署jrebel失败

    Idea热部署jrebel

  10. BZOJ1652 [Usaco2006 Feb]Treats for the Cows

    蒟蒻许久没做题了,然后连动规方程都写不出了. 参照iwtwiioi大神,这样表示区间貌似更方便. 令f[i, j]表示i到j还没卖出去,则 f[i, j] = max(f[i + 1, j] + v[ ...