761. Special Binary String

题意:

一个符合以下两个要求的二进制串:

\(1.串中包含的1和0的个数是相等的。\)

\(2.二进制串的所有前缀中1的个数不少于0的个数\)

被称为特殊二进制串

要求我们任意交换两个相邻的特殊二进制串(可以交换任意次)使得最终得到的序列的字典序最大,并且满足是特殊二进制串。

思路:

我们可以将1看作是上升,0是下降,那么可以这样

然后我们可以找最外层符合要求的子串也就是最底部从水平线开始又降到水平线的段,可以发现找这样的串只要统计一个cnt变量,然后cnt第一次从1变到0就可以找到一段。我们发现这些端除去首首尾,‘1’+str+‘0’,那么我们证明str是以1开头和0结尾的特殊串假设str以1结尾那么‘1’+str 的cnt计数为1,那么去除str最后的1,cnt = 0,那么与选择的串为第一个cnt=0不符,那么假设str首个为0,那么10为第一个cnt = 0所选的字段与当前不符,又前特殊串前后剥去1,0不改变特殊性。

那些段就可以看成是原问题的一个子问题,所以我们就可以递归处理了。由于交换特殊串的时候串的前缀和始终满足要求,所以只要sort下就可以了。

题链

代码:

class Solution
{
public:
string makeLargestSpecial(string S)
{
int len = S.length();
int cnt = 0;
vector<string>str;
int pr = 0;
for(int i = 0;i < len ;i++)
{
if(S[i] == '1')
cnt++;
else cnt--;
if(cnt==0)
{ //printf("%d\n",i);
str.push_back('1' + makeLargestSpecial(S.substr(pr+1,i-pr)) + '0');
pr = i+1;
} }
sort(str.begin(),str.end(),cmp);
string ask = "";
for(int i = 0;i < str.size();i++)
ask += str[i];
return ask;
}
static bool cmp(string a,string b)
{
return a > b;
}
};

leetcode 761. Special Binary String的更多相关文章

  1. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  2. 761. Special Binary String

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

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

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

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

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

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

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

  6. 【leetcode】1023. 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, r ...

  7. #Leetcode# 1016. Binary String With Substrings Representing 1 To N

    https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ Given a binary stri ...

  8. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  9. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

随机推荐

  1. CentOS6安装Zabbix(RPM包)

    1. 系统环境状态 2. 安装zabbix4.0 3. 安装mysql+apache+php环境 4.配置mysql 5.配置zabbix-server 6. 配置apache 7. web安装 1 ...

  2. LeetCode二维数组中的查找

    LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...

  3. 日常Java 2021/10/25

    ArrayList存储数字 import java.util.ArrayList; public class Arr_test { public static void main(String[] a ...

  4. day02 web主流框架

    day02 web主流框架 今日内容概要 手写简易版本web框架 借助于wsgiref模块 动静态网页 jinja2模板语法 前端.web框架.数据库三种结合 Python主流web框架 django ...

  5. celery开启worker报错django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE o

    其实挺简单的问题,但花了自己一个下午来解决,先是浏览各种博客,无果:没办法,然后去看celery官方文档,无果,近乎绝望,最后仔细看代码,找到问题所在(如下),自学狗这效率...... 下面是自己ta ...

  6. react动态添加样式:style和className

    react开发过程中,经常会需要动态向元素内添加样式style或className,那么应该如何动态添加呢??? 一.react向元素内,动态添加style 例如:有一个DIV元素, 需要动态添加一个 ...

  7. 了解 Linkerd Service Mesh 架构

    从较高的层次上看,Linkerd 由一个控制平面(control plane) 和一个 数据平面(data plane) 组成. 控制平面是一组服务,提供对 Linkerd 整体的控制. 数据平面由在 ...

  8. Templates and Static variables in C++

    Function templates and static variables: Each instantiation of function template has its own copy of ...

  9. C语言编辑链接

    库函数(Library Files)库函数就是函数的仓库,它们都经过编译,重用性不错.通常,库函数相互合作,来完成特定的任务.比如操控屏幕的库函数(cursers和ncursers库函数),数据库读取 ...

  10. 1.Thmeleaf模板引擎

    1.Thmeleaf的基本语法 大部分的Thmeleaf表达式都直接被设置到HTML节点中,作为HTML节点的一个属性存在,这样同一个模板文件既可以使用浏览器直接打开,也可以放到服务器中用来显示数据, ...