codeforc 603-A. Alternative Thinking(规律)
2 seconds
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.
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 a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
8
10000011
5
2
01
2
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(规律)的更多相关文章
- Codeforces 603A - Alternative Thinking - [字符串找规律]
题目链接:http://codeforces.com/problemset/problem/603/A 题意: 给定一个 $01$ 串,我们“交替子序列”为这个串的一个不连续子序列,它满足任意的两个相 ...
- Alternative Thinking 找规律
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO ...
- cf Round 603
A.Alternative Thinking(思维) 给出一个01串,你可以取反其中一个连续子串,问取反后的01子串的最长非连续010101串的长度是多少. 我们随便翻一个连续子串,显然翻完之后,对于 ...
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- hdu1452 Happy 2004(规律+因子和+积性函数)
Happy 2004 题意:s为2004^x的因子和,求s%29. (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...
- 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 ...
- ACM/ICPC 之 DP解有规律的最短路问题(POJ3377)
//POJ3377 //DP解法-解有规律的最短路问题 //Time:1157Ms Memory:12440K #include<iostream> #include<cstring ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- 在sqlserver中做fibonacci(斐波那契)规律运算
--利用sqlserver来运算斐波那契规律 --利用事物与存储过程 declare @number intdeclare @A intdeclare @B intdeclare @C int set ...
随机推荐
- 计算机网络-4-7-内部网关协议OSPF
内部网关协议OSPF(开放最短路径优先) 出现的原因:为了克服RIP协议的缺点在1989年开发出来,**开放 表明OSPF协议不受任何厂家的限制.最短路径优先是因为使用了最短路径算法SPF**. OS ...
- CSS上下左右居中对齐
上下左右居中对齐 display: inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...
- 为什么重写equals必须重写hashCode
目录 equals常见面试题 为什么要重写equals 重写equals不重写hashCode会存在什么问题 总结 equals常见面试题 在开始聊之前,我们先看几个常见的面试题,看看你能不能都回答上 ...
- javaSE高级篇2 — 流技术 — 更新完毕
1.先认识一个类----File类 前言:IO相关的一些常识 I / O----输入输出 I 输入 input 0 输出 output I / o 按数据的流动方向来分- ...
- 零基础学习java------day1------计算机基础以及java的一些简单了解
一. java的简单了解 Java是一门面向对象编程语言,不仅吸收了C++的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态 ...
- 纯CSS圆环与圆
1. 两个标签的嵌套: <div class="element1"> <div class="child1"></div> ...
- pyqt5 的串口编写进度
2020.12.18 今天遇到一个问题, 想用回车实现串口数据的发送. 下面这句话是让光标移动到文字的尾部,但是不能够实现. 对QTextEdit控件中的文字改写,或清除后,再调用下面的移动到尾部,就 ...
- 100个Shell脚本——【脚本8】每日生成一个文件
[脚本8]每日生成一个文件 要求:请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中,(不用考虑c ...
- Linux学习 - 数值运算
1 declare 声明变量类型 declare [+/-] [选项] 变量名 - 给变量设定类型属性 + 取消变量的类型属性 -i 将变量声明为整数型 -x 将变量声明为环境变量(同export) ...
- 【Linux】【Shell】【Basic】文件查找locate,find
1.locate: 1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb): 1.2. 工作特性:查找速度快:模糊 ...