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. 36-Same Tree

    Same Tree My Submissions QuestionEditorial Solution Total Accepted: 126116 Total Submissions: 291884 ...

  2. 学习java的第二十七天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  3. Java 性能优化的 50 个细节

    在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. #尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间, ...

  4. tomcat结合nginx

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  5. Android给页面添加横线和竖线

    竖线 <View      android:layout_width="1dip"     android:layout_height="match_parent& ...

  6. spring注解-bean生命周期

    https://www.jianshu.com/p/70b935f2b3fe bean的生命周期 bean创建---初始化----销毁的过程 容器管理bean的生命周期 对象创建:容器启动后调用bea ...

  7. Ruby Gems更换淘宝源方法

    官方的 Rubygems 源由于有些资源放在 Amazon S3 上面,所以有时会抽风,在 Linux 下我用 proxychains gem install xxx 实现了指定程序实行 Shadow ...

  8. VueAPI 2 (生命周期钩子函数)

    所有的生命周期钩子自动绑定 this 上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法. beforeCreate 在实例初始化之后,此时还不 ...

  9. OceanBase 2.x体验:推荐用DBeaver工具连接数据库

    Original MQ4096 [OceanBase技术闲谈](javascript:void(0) 2020-01-15 OceanBase 2.x体验:推荐用DBeaver工具连接数据库 Ocea ...

  10. 【C/C++】最长不下降子序列/动态规划

    #include <iostream> #include <vector> using namespace std; int main() { //输入 int tmp; ve ...