题意

给出一个 N 位的 01 串

然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0)

然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 这样的 子串

思路

其实 我们可以发现 连续的 010 子串 有两种情况

第一种: 0101010 这样包含连续的

第二种: 010010 这样 分开的

对于第一种 我们改 两个 1 之间的 0

对于第二种 我们改 两个0 之间 的 1

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6; const int INF = 0x3f3f3f3f;
const int maxn = 1e9 + 5;
const int MOD = 1e9 + 7; int main()
{
int n;
string s;
cin >> n >> s;
int m = n - 1;
int ans = 0;
for (int i = 1; i < m; i++)
{
if (s[i] == '1' && s[i - 1] == '0' && s[i + 1] == '0')
{
ans++;
if ((i + 3) <= m && s[i + 2] == '1' && s[i + 3] == '0')
s[i + 1] = '1';
else
s[i] = '0';
}
}
cout << ans << endl;
}

HackerRank - beautiful-binary-string 【字符串】的更多相关文章

  1. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  2. perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string

    encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...

  3. [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  4. [Swift]LeetCode1016. 子串能表示从 1 到 N 数字的二进制串 | Binary String With Substrings Representing 1 To N

    Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return ...

  5. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  6. Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心

    D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...

  7. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  8. D1. Kirk and a Binary String (easy version)

    D1. Kirk and a Binary String (easy version) 01串找最长不降子序列 给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致 从后 ...

  9. CF1400-C. Binary String Reconstruction

    CF1400-C. Binary String Reconstruction 题意: 对于一个二进制字符串\(s\),以及一个给定的\(x\),你可以通过一下操作来得到字符串\(w\): 对于字符串\ ...

  10. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Oracle 格式化中文时间

    SELECT TO_CHAR(SYSDATE, 'MM"月"DD"日"') A FROM dual; --注: 当格式字符串当中, 存在 自定义的字符信息的时候 ...

  2. rabbitmq 用户和授权

    官方文档 https://my.oschina.net/hncscwc/blog/262246?p=

  3. JQuery DataTables学习

    1.Datatables简单介绍 DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,根据的基础逐步增强,这将添加先进的互动控制.支持不论什么HTML表格. 主要特点: 自己主动 ...

  4. Centos7 搭建最新 Nexus3 Maven 私服

    Maven 介绍 Apache Maven 是一个创新的软件项目管理和综合工具.Maven 提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文 ...

  5. c#调用WinRAR软件压缩和解压文件

    using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...

  6. Github上的PHP开源资源汇总

    依赖管理 ——用于依赖管理的包和框架 Composer/Packagist : 一个包和依赖管理器 Composer Installers:  一个多框架Composer库安装器 Pickle: 可以 ...

  7. mac权限

    mac文件后面出现@权限 去除方法: xattr -c 文件名  目录也可以

  8. php正則表達式中的非贪婪模式匹配的使用

    php正則表達式中的非贪婪模式匹配的使用 通常我们会这么写: $str = "http://www.baidu/.com? url=www.sina.com/"; preg_mat ...

  9. MapReduce Input Split 输入分/切片

    MapReduce Input Split(输入分/切片)详解 public static long getMaxSplitSize(JobContext context) { return cont ...

  10. openwrt修改密码

    默认情况下root是没有密码的 需要设置密码后才能开启ssh 修改/etc/shadow文件: root:$1$wEehtjxj$YBu4quNfVUjzfv8p/PBo5.:0:0:99999:7: ...