题目链接:http://codeforces.com/problemset/problem/603/A

题意:

给定一个 $01$ 串,我们“交替子序列”为这个串的一个不连续子序列,它满足任意的两个相邻的数字不相等。

现在,我们要对这个 $01$ 串的某一段非空连续子串进行反转操作,即将这一段上的所有 $0$ 变为 $1$,所有 $1$ 变为 $0$。

然后,求问进行了有且仅有一次的反转操作后,求该串的最长交替子序列的长度。

题解:

首先,对于一个 $01$ 串,对其进行压缩操作,即将所有连续的相同的数字压缩成一个数字,例如将 $10110011$ 可以压缩成 $10101$,然后得到的这个新的串就是一个交替子序列,并且是最长的那个。

而且不难发现,对于任何 $01$ 串,一次反转操作,可以使其最长交替子序列的长度增加 $0,1,2$:

1、本身就是一个交替子序列,增加 $0$。

2、包含一个“$00$”或者“$11$”,能增加 $1$。

3、包含超过一个的“$00$”或者“$11$”,能增加 $2$(注意这种情况,两个“$00$”重叠在一起得到“$000$”也算)。

换句话说,我们只要统计所有满足 $s[i]=s[i+1](i \in [1,n-1])$ 的 $i$ 的个数就能知道有多少个“$00$”或者“$11$”。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main()
{
cin>>n>>s; int cnt=;
for(int i=;i<n-;i++) if(s[i]==s[i+]) cnt++; int res=, now=s[];
for(int i=;i<n;i++) if(s[i]!=now) res++, now=s[i]; if(cnt>=) cout<<res+<<endl;
else if(cnt==) cout<<res+<<endl;
else cout<<res<<endl;
}

Codeforces 603A - Alternative Thinking - [字符串找规律]的更多相关文章

  1. codeforces B. A and B 找规律

    Educational Codeforces Round 78 (Rated for Div. 2) 1278B - 6 B. A and B  time limit per test 1 secon ...

  2. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  3. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  4. Codeforces Gym 100637B B. Lunch 找规律

    B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...

  5. Codeforces 474D Flowers (线性dp 找规律)

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  6. Codeforces 603A Alternative Thinking

    题意:给你一个01串,必须替换一次,且替换的为子串.问换完后,最大01串长度. #include <bits/stdc++.h> typedef long long ll; using n ...

  7. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

  8. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  9. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

随机推荐

  1. MySQL 四种事务隔离级的说明[转]

    很早之前写的文章,重新回顾和学习下: 按照SQL:1992 事务隔离级别,InnoDB默认是可重复读的(REPEATABLE READ).MySQL/InnoDB 提供SQL标准所描述的所有四个事务隔 ...

  2. Ubuntu菜鸟入门(十八)————解决Ubuntu下Sublime Text 3无法输入中文

    一.下载我们需要的文件,打开终端,输入: git clone https://github.com/lyfeyaj/sublime-text-imfix.git 二.将subl移动到/usr/bin/ ...

  3. Cannot attach the file as database

    Cannot attach the file as database这个异常是在EF的code frist里经常出现的,解决方法很简单,只要重新启动一下V11实例即可. CMD> sqlloca ...

  4. 1.关于Swift

    关于SwiftSwift是一种新的编程语言,用于iOS和OS X的应用程序,建立在最佳的C和Objective-C之上,没有C兼容性的限制.Swift采用的安全模式设计,并增加了现代的功能,使编程更简 ...

  5. 用H5上传文件

    //1,第一步读取用户选中的文件 <input type="file" accept="image/*" onchange = "selecte ...

  6. 测试覆盖率工具:EclEmma

    测试覆盖率工具:EclEmma 2016-08-26 目录 1 测试覆盖率实现技术2 EclEmma介绍3 EclEmma测试覆盖率指标4 EclEmma安装5 示例项目介绍  5.1 创建项目  5 ...

  7. idea git 使用

    第一部  测试  本地git 是否已经成功安装 centos 7 发行版默认已经安装 第二部: 测试 github 连接是否成功,需要输入用户密码 第三部:创建git项目管理两种方式 1. 菜单 VC ...

  8. MXNET:卷积神经网络基础

    卷积神经网络(convolutional neural network).它是近年来深度学习能在计算机视觉中取得巨大成果的基石,它也逐渐在被其他诸如自然语言处理.推荐系统和语音识别等领域广泛使用. 目 ...

  9. hdoj:2022

    #include <iostream> #include <string> using namespace std; int main() { int m, n; int x, ...

  10. NLog使用

    NLog的配置文件,文件上面有详细的备注,注意这个配置文件一定要放在NLog.dll的文件夹里 <?xml version="1.0" encoding="utf- ...