题意

给出一个 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. SDUT 2766-小明传奇2(母函数)

    小明传奇2 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limit 65536K ...

  2. java 实现统计某段文字在内容中出现的次数

    http://outofmemory.cn/code-snippet/815/java-zishutongji 一个api,位于apache.commons.lang.StringUtils类下的一个 ...

  3. 在Excel2003中给定区域填充随机数?

    1.选中一个单元格,输入“=RAND()*40+60”,点CTRL+ENTER键,即可完成公式填充. 2.鼠标悬停单元格边框右下角,出现“+”符号,左键拉动实现格式复制,从而填充整个区域随机数. ★题 ...

  4. 深入探析 Rational AppScan Standard Edition 新特性之 Glass Box 扫描

    众所周知,Web 应用安全测试通常有黑盒安全测试和白盒安全测试两种方法.这两种方法孰优孰劣一直众议纷纷.广为公认的是,这两种测试方法有着良好地互补性,两种测试方法的结合是未来安全测试技术的发展趋势.G ...

  5. SQL 根据表获取字段字符串

    SQLSERVER查询单个数据表所有字段名组合成的字符串脚本 --SQLSERVER查询单个数据表所有字段名组合成的字符串脚本 --应用场合: 用于生成SQL查询字符串中select 字段名列表1 f ...

  6. 本地文件上传到linux

    首先下载插件,输入下面命令: yum -y install lrzsz 然后输入rz选择上传文件: rz 如果rz命令不好使的话,就输入: rz -be

  7. 堆排序算法的java实现

         堆积排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,可以利用数组的特点快速定位指定索引的元素.堆排序是不稳定的排序方法,辅助空间为O(1), 最坏时间复杂度为O ...

  8. Google 全球 IP 地址库

    ## Google 全球 IP 地址库 IP 地址来源:http://www.kookle.co.nr Bulgaria 93.123.23.1 93.123.23.2 93.123.23.3 93. ...

  9. [译]GLUT教程 - 整合代码8

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...

  10. nginx proxy_pass 里的”/”

    见配置,摘自nginx.conf 里的server 段: server { listen 80; server_name abc.163.com ; location / { proxy_pass h ...