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 ...
- 【转】Intellij IDEA常用配置详解
1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...
- C#实现发送手机短信
- Android:Activity+Fragment及它们之间的数据交换(一)
简单介绍: 为什么要用Fragment?使用Fragment能够在一个Activity中实现不同的界面. Fragment与Fragment之间的动画切换,远比Activity与Activity之间的 ...
- TI_DSP_SRIO - package type(包格式)
1.The type of SRIO packageis determined by the combination of Ftype(format type) and Ttype(transacti ...
- jquery的eq()
jQuery 遍历 - eq() 方法 jQuery 遍历参考手册 实例 通过为 index 为 2 的 div 添加适当的类,将其变为蓝色: $("body").find(&qu ...
- Active Directory的基本概念
前言 本文是面对准备加入Active Directory编程的初学者的一份文章,主要是讲解Active Directory(活动目录)的一些概念和相关知识.这篇文章本来是不想写下来的,因为概念性内容的 ...
- Apache + Tomcat集群 + 负载均衡
Part I: 取经处: http://www.ramkitech.com/2012/10/tomcat-clustering-series-simple-load.html http://blog ...
- 【Atheros】Ath9k速率调整算法源码走读
上一篇文章介绍了驱动中minstrel_ht速率调整算法,atheros中提供了可选的的两种速率调整算法,分别是ath9k和minstrel,这两个算法分别位于: drivers\net\wirele ...
- 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用
和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...