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;}
};

转载于:https://www.cnblogs.com/zzuli2sjy/p/8260854.html

leetcode 761. Special Binary String相关推荐

  1. leetcode 67 Add Binary

    Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions Given two binary strings, ...

  2. [LeetCode]67.Add Binary

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

  3. Java for LeetCode 067 Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  4. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  5. LeetCode 98. Validate Binary Search Tree--C++解法--判断是否是BST--递归,迭代做法,中序遍历

    LeetCode 98. Validate Binary Search Tree–C++解法–判断是否是BST–递归,迭代做法,中序遍历 LeetCode题解专栏:LeetCode题解 LeetCod ...

  6. LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  7. leetcode算法题--Decode String

    原题链接:https://leetcode.com/problems/decode-string/ string decodeString(string s) {int n = s.size(), i ...

  8. [LeetCode] 67. Add Binary Java

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

  9. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  10. Binary String Reconstruction CodeForces - 1352F(思维+构造)

    For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...

最新文章

  1. http各种中的各种timeout
  2. Hashcat从入门到入土(一)
  3. fastai学习:05_pet_breeds Questionnaire
  4. element tree树组件default-expand-all属性设置默认展开的节点
  5. web自动化框架抽取示例【Java+selenium】
  6. 点云自适应滤波matlab代码,散乱点云自适应滤波算法
  7. verilog教程——initial语句和always语句
  8. 林子雨_大数据技术原理与应用课后习题
  9. gatk过滤_详解GATK突变硬过滤 | 群体遗传专题
  10. RK3568 Android12 移除电池图标和设置选项
  11. 17-11-01模拟赛
  12. TVS二极管DO-15封装型号,有哪些?
  13. oracle存储多少条数据类型,Oracle目前可以存储极大的对象,这是因为它引入了四种新的数据类型。其中哪一种大对象数据类型在数...
  14. STM32+ESP-01对接阿里飞燕平台
  15. 大数据安全分析平台评估五要素
  16. 学习记录 --【零基础CSS学习】12.字体属性
  17. 财务自由,财务不自由,人生幸福
  18. Python常见问题解决记录1-Non-ASCII character '\xe7'错误
  19. [生存志] 第65节 曾参公西赤
  20. 计算机应用系发展规划,计算机应用专业大学生职业生涯规划书范文2000字

热门文章

  1. [第五组] 典型用户 +用例+功能说明书+技术说明书 2017.07.25版
  2. Sqoop 使用指南
  3. navicat 连接mysql is not allowed to connect to this mysql server
  4. 思科GNS3和华为eNSP完美结合
  5. XFBAY学习笔记=++=实验十三 交换机和路由器组合实验
  6. 稳的一比,鸿蒙系统霸榜Github!
  7. RabbitMQ 线上事故!慌的一批,脑袋一片空白……
  8. Redis如何实现故障自动恢复?
  9. 8 种单例模式写法,助你搞定面试!
  10. 互联网项目架构经验分享