codeforces604C Alternative Thinking (脑洞大开 )

题目连接: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值