A. Alternative Thinking
time limit per test

2 seconds

memory limit per test

256 megabytes

 

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.

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.

http://codeforces.com/problemset/problem/603/A

题意:给你一串01串。

然后你可以选其中的一个连续子串,然后反转一下,就是0变1,1变0,然后求01相互间个的子序列最长是多少。

如果你选一串子串,反转,改变的只是子串两端的关系,子串内部的结构不会改变,比如1001001,变为0110110,那么内部的01还是原来的个数,

那么好了,改变前后01最多增加2个,可以假设原来已经成01或10 的不变(贪心原则)那么只要把那些11,和00,的改变,

计算前后两个相等的个数,然后和2比较取小的就行,再加上原来的01个数,就行了.

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<iostream>
5 using namespace std;
6 char a[100005];
7 int main(void)
8 {
9 int i,j,k,p,q;
10 while(scanf("%d",&k)!=EOF)
11 {
12 scanf("%s",a);
13 int sum=0;
14 int cnt=0;
15 for(i=1; a[i]!='\0'; i++)
16 {
17 if(a[i]==a[i-1])
18 {
19 sum++;
20 }
21 else cnt++;
22
23 }
24 int r=min(2,sum);
25 printf("%d\n",cnt+r+1);
26
27 }
28 return 0;
29
30 }

codeforc 603-A. Alternative Thinking(规律)的更多相关文章

  1. Codeforces 603A - Alternative Thinking - [字符串找规律]

    题目链接:http://codeforces.com/problemset/problem/603/A 题意: 给定一个 $01$ 串,我们“交替子序列”为这个串的一个不连续子序列,它满足任意的两个相 ...

  2. Alternative Thinking 找规律

    Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...

  3. cf Round 603

    A.Alternative Thinking(思维) 给出一个01串,你可以取反其中一个连续子串,问取反后的01子串的最长非连续010101串的长度是多少. 我们随便翻一个连续子串,显然翻完之后,对于 ...

  4. 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)

    坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...

  5. hdu1452 Happy 2004(规律+因子和+积性函数)

    Happy 2004 题意:s为2004^x的因子和,求s%29.     (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...

  6. Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)

    传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...

  7. ACM/ICPC 之 DP解有规律的最短路问题(POJ3377)

    //POJ3377 //DP解法-解有规律的最短路问题 //Time:1157Ms Memory:12440K #include<iostream> #include<cstring ...

  8. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  9. 在sqlserver中做fibonacci(斐波那契)规律运算

    --利用sqlserver来运算斐波那契规律 --利用事物与存储过程 declare @number intdeclare @A intdeclare @B intdeclare @C int set ...

随机推荐

  1. Hive(十一)【压缩、存储】

    目录 一.Hadoop的压缩配置 1.MR支持的压缩编码 2.压缩参数配置 3.开启Mapper输出阶段压缩 4.开启Reduceer输出阶段 二.文件存储 1.列式存储和行式存储 2.TextFil ...

  2. CORS 如果需要指定多个域名怎么办

    CORS 通过控制 Access-Control-Allow-Origin 控制哪些域名可以共享资源,取值如下 Access-Control-Allow-Origin: <origin> ...

  3. python下载openpyxl

    直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...

  4. Lock锁的使用

    在Java多线程中,可以使用synchronized关键字实现线程之间的同步互斥,在jdk1.5后新增的ReentrantLock类同样可达到此效果,且在使用上比synchronized更加灵活. 观 ...

  5. mysql 索引 零记

    索引算法 二分查找法/折半查找法 伪算法 : 1. 前提,数据需要有序 2. 确定数据中间元素 K 3. 比如目标元素 A与K的大小 3.1 相等则找到 3.2  小于时在左区间 3.3  大于时在右 ...

  6. Linux服务器---论坛discuz

    Discus Discuz是一款免费的论坛管理系统,大量的使用了AJAX,内部有丰富的模板风格. 1.下载discuz软件(https://cn.wordpress.org/download/rele ...

  7. activiti工作流引擎

    参考文章 Activiti-5.18.0与springMvc项目集成和activiti-explorer单独部署Web项目并与业务数据库关联方法(AutoEE_V2实现方式) https://blog ...

  8. supervise安装与使用

    确认当前是否已经安装which supervise/usr/local/bin/supervise 软件下载安装-------------------------------------------- ...

  9. Mybatis通用Mapper介绍和使用

    Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...

  10. 【C/C++】日期问题/算法笔记/入门模拟

    最近把算法竞赛入门经典的前半部分看完了,开始看算法笔记入门算法. 看了前半部分的例题,很多是算法竞赛入门经典中出现过的,但是感觉这本书写的更适合初学者,而且真的很像考试笔记,通俗易懂. //日期问题 ...