Codeforces Round #334 (Div. 2) 604C Alternative Thinking(脑洞)

详解AUTOSAR:DaVinci Configurator导入ARXML配置(拓展篇—2) DaVinci Configurator导入ARXML配置。 阅读详情

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 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.



题目链接:点击打开链接

给出一个字符串, 可以将连续的0变为1, 问形如"010" "101" "1010"类的字符串最长多长.

若字符串长度小于4, 则字符串最长的长度为字符串本身长度. 否则记录 s[i] != s[i - 1]数量, 加到ans上, ans初始化为3, 因为连续的000可

以变为010, 即长度为3. 最后如果求得的答案大于本身长度, 则答案为字符串本身长度.

AC 代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5;
int n, ans = 3;
char s[MAXN];
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    scanf("%s", s);
    for(int i = 1; i < n; ++i)
        ans += (s[i] != s[i - 1]);
    if(ans > n) ans = n;
    printf("%d\n", ans);
    return 0;
}



【数据集】全球预报系统GFS概述:数据下载及处理 阅读详情

相关推荐

IFIX数据库Datebase当前值问号

IFIX数据库Datebase当前值问号,已解决,亲测有效 在OPC PowerTool有数据的情况下,IFIX数据库Datebase当前值显示???? 问题分析及解决方法 我的IFIX5.8版本,其它版本可参考 打开IFIX工作台:应用程序–任务控制–I/O驱动–OPC Client;查看OPC驱动是否被禁用,点击启动,变成激活状态即可;若点击启动无法激活OPC驱动,需要重新安装OPC驱动,方法如下: 下载I/O Driver文件 下载链接:https://download.csdn.net/down

Ron_Tso的博客 2759

Codeforces 603A Alternative Thinking(字符串规律)

@(K ACMer)题意: 给你一个01字符串,你必须翻转其中的一个子串,问执行该操作后该字符串的最长交替子序列长度为多少? 分析 首先显然翻转整个字符串该字符串的01子序列长度不变,所以最次也是不变不会减小 然后我们队字符串进行压缩把连续的0和,连续的1压缩为一个,并记录它们各自压缩前的连续字符的个数。 显然交替子序列的个数最多增加两个,我们定义"00","11"这样的字符串叫做对。发现

JIBANCANYANG 1069

车载以太网测试技术解析

在智能汽车与HiL测试领域,高效解析与模拟车载网络通信是开发验证的关键。ZD-Converter 2000以媒体/介质转换器、嗅探器、拦截器、仿真器四合一的颠覆性设计,成为工程师应对复杂车载以太网测试的瑞士军刀。它支持双路车载/标准以太网智能转换、实时通信嗅探、硬件级数据拦截及ECU通信模拟,更通过串口命令实现全参数远程配置,赋能自动化测试流程。本文将深度解析其核心功能与实战应用场景。

Triv2025的博客 1142

CodeForces 604C Alternative Thinking(思维题)

题目链接:点击打开链接 题意:给出01字符串。可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所有的位取反。求经过处理后最多会得到多少次01变换(可以不连续)。 思路:思维题。模拟可以发现,增加01变换的方式有大致三种,一是在连续相同的字符串(这种子串记为s)内部找子串取反,这种能够增加01变换的s长度要大于等于3,如10001->10101;二是在s与其相邻非s取相应的部分取反,如

不忘初心 487

Dify使用教程(创建应用)

Dify的安装部署我已经写过了,简单的模型配置我也在前面进行了讲解,今天我们主要来讲讲如何使用Dify。

wal1314520的博客 1万+

《哥谭神话-工程篇》02.Palantir的Ontology层(语义层) -企业操作系统的大脑

Ontology(语义层) 是Foundry 的核心层。Ontology 位于集成到 Foundry 中的数字资产(数据集和模型)之上,并将其连接到现实世界的对应物,从工厂、设备和产品等物理资产到客户订单或金融交易等概念。本篇将详细拆解Ontology(语义层)。

883

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 standar

hkh746392783的专栏 1013

UR机械臂正逆运动学解析:从DH参数到8组解的完整求解策略

本文深入解析UR机械臂的正逆运动学求解策略,从DH参数原理到完整的8组解求解方法。详细介绍了正运动学的坐标变换过程,以及逆运动学中Atan2函数和三角恒等变换的应用,并揭示了8组解的产生机制。通过实际案例和调试技巧,帮助工程师掌握UR机械臂运动学求解的核心技术。

weixin_29094775的博客 413

CodeForces 603A Alternative Thinking 题解

【题目大意】: 给你一个字符串,你可以使得某一个连续的01串翻转过来(将0变1,1变0),问最长的01相隔的子序列(可不连续,关于不连续可见样例)的长度为多少(原题及样例见下) Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes inp

初学者 1646

双相机坐标系标定

工业双相机对位标定

sinat_33762356的博客 8317

使用 Simulink + Simscape Electrical 搭建一个三相PWM整流器模型

本文详细介绍了使用Simulink搭建三相PWM整流器模型的方法,重点讲解了SVPWM调制策略的实现过程。内容包括系统结构设计、主电路搭建、SVPWM算法实现、双闭环控制器设计以及仿真分析。通过MATLAB Function Block实现SVPWM核心算法,并给出优化建议,如死区补偿、开关频率优化等。该教程为电力电子领域工程师提供了实用的建模与仿真方法,可用于新能源并网、电机驱动等应用场景的研发与优化。

amy_mhd的博客 196

Codeforces 604C - Alternative Thinking(思维)

C. Alternative Thinking time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Kevin has just recevied his disappointing results on the USA Ident

Snakeflute的博客 802

C++如何实时监测另一个进程是否已经退出?(附源码)

调用WaitForSingleObject实时监测目标进程是否已经退出

dvlinker的技术专栏 8275

Codeforces Round #334 (Div. 2) C. Alternative Thinking

题目链接:http://codeforces.com/contest/604/problem/C 大致题意:给定一个01串以及其长度,求此串中符合形如“0101“或”101“这种01交错的序列(不必连续)的长度,并且你可以选定这个串中1段连续的子串进行反转(将0反转为1,将1反转为0),力求反转之后的串中所符合题意的序列的长度最大,并输出其长度。 (题目中”he decides that he...

weixin_30376509的博客 70

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Codeforces Round #334 (Div. 2) C. Alternative Thinking 贪心

C. Alternative Thinking Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/problem/C Description Kevin has just recevied his disappointing results on the USA Identifi...

weixin_34072458的博客 185

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Alternative Thinking CodeForces - 604C(思维)

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 sc

SunMoonVocano的博客 424

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Codeforces 604 C Alternative Thinking【思维】

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

mengxiang000000 1329

【Codeforces Round 334 (Div 2)C】【脑洞】Alternative Thinking 最多反转一个区间使得最长跳跃子串的长度尽可能长

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 disappointin

snowy_smile的博客 987

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 sco

海绵的博客 405

D - 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 sco

philophobia的博客 483

CodeForces 603A Alternative Thinking

A. 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 res

lizhaowei213的博客 985

Codeforces Round #334 (603A) 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. —— 给出一个01串,先在你可以取反其中一个Substring(0变1,1变0),问这样操作后最长的0,1间隔子序列最长是多少。

Asuna_acmer 888

Alternative Thinking(CodeForces - 604C)(思维)

Alternative Thinking(CodeForces - 604C)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

reset的博客 531

Codeforces Round #334 (Div. 1)

自从CF的rating改版以后终于回到DIV1了。打了一场还比预期好,如果C题不错的话应该可以继续上分,可惜了。 Codeforces 603A. Alternative Thinking 题目链接:http://codeforces.com/problemset/problem/603/A 题意:给你一个01串,要你选择其中一个非空的01子串,将其反转(0变成1,1变成0),要

Fry_guest的专栏 1222

Codeforces Round #334 (Div. 1) ABC

A Alternative Thinking         贪心。从左到右扫,找到最先出现的 和上个字符相同的 字符(连续的11或00)。从这个字符开始,只要它和上个字符相同,就反转,再往后考察,反转尽量多,直到字符相异时停止。最后统计一下即可。 #include using namespace std; #define ll long long char str[100010]

A Cube Master 717

Codeforces Round #334 (Div. 2)C. Alternative Thinking

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 disappo

走一步再走一步 710
上一篇: Educational Codeforces Round 2 600C Make Palindrome(脑洞)
下一篇: Codeforces Round #334 (Div. 2) 604D Modular Arithmetic(数学+快速幂)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值