HackerRank - beautiful-binary-string 【字符串】
题意
给出一个 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 【字符串】的更多相关文章
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- 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 ...
- [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [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 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- 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 ...
- 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 ...
- D1. Kirk and a Binary String (easy version)
D1. Kirk and a Binary String (easy version) 01串找最长不降子序列 给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致 从后 ...
- CF1400-C. Binary String Reconstruction
CF1400-C. Binary String Reconstruction 题意: 对于一个二进制字符串\(s\),以及一个给定的\(x\),你可以通过一下操作来得到字符串\(w\): 对于字符串\ ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 每天一个JavaScript实例-展示设置和获取CSS样式设置
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- WebService的缓存机制
分类: .NET ASP 2010-11-13 10:00 849人阅读 评论(0) 收藏 举报 webservicestring数据库nullapplicationweb服务 WebService的 ...
- 后期给项目加入Git版本控制
一.为项目加上Git 1.进入对应文件夹 2.git init(初始化一个空的代码仓库) 3.git add .(将当前目录和子目录的文件标记为要添加到代码仓库) 4.git commit -m &q ...
- 使用itchat分析自己的微信(1)
1.准备工作 安装itchat pip install itchat参考 itchat内部函数 2.个人微信男女比例分析 ''' Have fun:itchat Author:杨 景 Time:201 ...
- 转: Genymotion使用及离线镜像的安装
Genymotion是一套完整的工具,它提供了Android虚拟环境.由于它比Android自带的模拟器要流畅好多,所以它简直就是开发者.测试人员.推销者甚至是游戏玩家的福音.支持Windows.Li ...
- 解决Linux上解压jdk报错gzip: stdin: not in gzip format
最近在阿里上买了个服务器玩,需要安装jdk,在解压过程中遇到了一些问题,又是一番Google度娘,终于解决了.问题原因让我有点无奈…… 输入 #tar -xvf jdk-8u131-linux-x64 ...
- Atitit.pdf 预览 转换html attilax总结
Atitit.pdf 预览 转换html attilax总结 1. Swf flash还是html1 2. pdf2htmlEX1 3. iText 5.5.0 发布,Java 的 PDF 操作类库1 ...
- springBoot+springCloud学习笔记
尊重原创:https://www.jianshu.com/p/492dfefa2735 SpringBoot 配置优先级 在命令行中传入的参数 如:java -jar storeMs.jar --se ...
- linux系统寻找僵尸进程
1. 用top命令来查看服务器当前是否有僵尸进程. 2. 用ps和grep命令寻找僵尸进程 $ ps -A -ostat, pid, ppid, cmd | grep -e '^[Zz]' 命令解释: ...
- js 判断 IE 浏览器
遇到一些IE兼容问题,可以考虑在该浏览器环境下,用js控制样式,以下是判断IE版本的js代码 var browser=navigator.appName var b_version=navigator ...