Alternative Thinking 找规律
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
8
10000011
5
2
01
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 找规律的更多相关文章
- hdu 3951 - Coin Game(找规律)
这道题是有规律的博弈题目,,, 所以我们只需要找出规律来就ok了 牛人用sg函数暴力找规律,菜鸟手工模拟以求规律...[牢骚] if(m>=2) { if(n<=m) {first第一口就 ...
- HDU 5703 Desert 水题 找规律
已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这 ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- CF456B Fedya and Maths 找规律
http://codeforces.com/contest/456/problem/B CF#260 div2 B Fedya and Maths Codeforces Round #260 B. F ...
- hdu 4731 2013成都赛区网络赛 找规律
题意:找字串中最长回文串的最小值的串 m=2的时候暴力打表找规律,打表可以用二进制枚举
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
- 找规律 ZOJ3498 Javabeans
Javabeans are delicious. Javaman likes to eat javabeans very much. Javaman has n boxes of javabeans. ...
- C基础之递归(思想很重要,学会找规律)
递归思想的条件:1.函数自己调用自己 2.函数必须有一个固定的返回值(如果没有这个条件会发生死循环) ----规律很重要 简单递归题目一: 设计一个函数计算一个整数的n次方,比如2的3次方,就是8 步 ...
- BZOJ-1228 E&D 博弈SG+找啊找啊找规律
讨厌博弈,找规律找半天还是错的.... 1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MB Submit: 666 Solv ...
随机推荐
- 使用排序数组/链表/preorder构建二叉搜索树
2018-08-13 11:29:05 一.Convert Sorted Array to Binary Search Tree 问题描述: 问题求解: public TreeNode sortedA ...
- Ubuntu安装NVIDA显卡驱动
0. 综述 电脑型号:R720 Ubuntu版本:16 显卡型号:1050ti 目前,知道3种安装N卡驱动的方法: 1. PPA源:最简便,但未必有最新驱动(亲测),或可能遇到问题(风闻). sudo ...
- Yii中的CComponent应用实例
首先我们先了解一下如何创建一个CComponent,手册讲述如下: CComponent 是所有组件类的基类. CComponent 实现了定义.使用属性和事件的协议. 属性是通过getter方法或/ ...
- 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(一)
源自:http://blog.163.com/zwx_gis/blog/static/32434435201122193611576/ (主页:http://blog.163.com/zwx_gis/ ...
- (转)代号为Purley的新一代服务器平台
英特尔(Intel)正式发布了代号为Purley的新一代服务器平台,包括代号为Skylake的新一代至强(Xeon)CPU,命名为英特尔至强可扩展处理器(Intel Xeon Scalable Pro ...
- President's Path CodeForces - 416E (最短路,计数)
大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...
- ccf交通规划
一.试题 问题描述 G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统. 建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改 ...
- .net 外部CSS文件不起作用总结
外部css文件样式全部不起作用 asp.net 页面引用路径的问题 缺少必须属性<link rel="stylesheet" type="text/css" ...
- OAF多语言的实现
在之前的文章中转载了一篇关于多语言实现的文章. OAF中多语言的实现(转) 现在我来讲讲另外一种情况. 在建立工程项目没有特别选择语言,所以所有的PG或RN页面都是英文的,在页面原文件中可看到如下内容 ...
- 实现自己的ls命令
一步步实现,先看最简单的ls的指令: ls不带参数,直接打印文件名 dst.txt main10.c main11.c main12.c main13.c main14.c~ main15.c~ ma ...