您的位置:首页 > 编程语言 > C语言/C++

codeforces604C Alternative Thinking (脑洞大开 )

2015-12-03 21:45 330 查看
题目连接:http://codeforces.com/contest/604/problem/C 
C. 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
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.

题目大意:给你一串长度为n的字符串,由 0 和 1 组成,我们可以翻转连续的一段,然后求最长字串,字串须满足相邻两个字符不能相等

这个就是脑洞大开题目,假如有三个连续的数,那么最终长度可以增加2 ,如果只有两个 则增加1 ,比如11000,可以变成11010,比如1100 可以变成 1010,比如11010100 可以变成 10101010  不过增长的最长只有2 

我的代码写的有点纠结 可以参考以下

#include <stdio.h>
#include <string.h>
int main()
{
int n;
char st[100010]; //字符
while (~scanf("%d",&n ) )
{
scanf("%s",st);
int res = 1; //结果
int temp ; //记录中间的变量
int ans = 0; //记录增加的数
int count = 1; //每一段相同数的个数
if (n <= 3 )
{
printf("%d\n",n);
continue;
}
temp = st[0];
for (int i = 1 ; i < n ; i++ )
{
if (st[i] != temp) //这个是统计 如果不翻转的最长字串
{
temp = st[i];
res++;
if ( count < 3) //相同个数大于等于3
{
ans += 1;
}
else if (count >= 3 ) //相同个数 大于等于2{
ans += 2;
}
count = 1;
}
else if (st[i] == st[i-1])
{
count ++;
}
}
if ( count > 1) //因为我代码的关系 所以结尾相同的话 我另外写出来了
{
if ( count >= 3)
{
ans += 2;
}
if (count < 3)
{
ans += 1;
}
}
if ( ans > 2) //因为最多只能增加2{
ans = 2;
}
res += ans;
if (res <= 3)
{
res = 3;
}
if (res > n) //长度不能超过n
{
res = n;
}
printf("%d\n",res );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ c语言 codeforces ACM