题解:CF1971D Binary Cut

题意

给予你一个 \(01\) 字符串,你可以将它分割,分割后必须排成先 \(0\) 后 \(1\) 的格式。

求最少分割为几部分。

思路

将 \(0\) 和 \(1\) 分离出来。

如:

010001 ->  0 1 000 1

求出一共有几部分,记为 \(sum\)。

接下来分类讨论:

  1. \(sum=1\),即只有 \(1\) 或 \(0\) 的序列,输出 \(1\)。
  2. \(sum=2\) 且开头为 \(1\),即需分成两段的序列,所以输出 \(2\)。
  3. 其它情况,输出 \(sum-1\),因为必有一对相邻的 \(1\) 和 \(0\) 相融合。

这道题就愉快的卡过去了。

代码

#include <bits/stdc++.h>
using namespace std;
int t,sum=0;
string s;
int main () {
cin>>t;
while(t--){
int truly=0,falsely=0;
sum=0;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
if(falsely==0)sum++;
falsely++;
truly=0;
}else{
if(truly==0)sum++;
truly++;
falsely=0; }
}
if(sum==1){
cout<<1<<"\n";
}else if(sum==2&&s[0]=='1'){
cout<<2<<"\n";
}else{
cout<<sum-1<<"\n";
}
}
return 0;
}

题解:CF1971D Binary Cut的更多相关文章

  1. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  2. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. [LeetCode 题解]: Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

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

  5. LeetCode(67)题解: Add Binary

    https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a bin ...

  6. LeetCode题解之Binary Tree Right Side View

    1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...

  7. LeetCode题解之Binary Tree Pruning

    1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...

  8. LeetCode题解Maximum Binary Tree

    1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...

  9. LeetCode题解之Binary Tree Paths

    1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root ...

  10. LeetCode题解之Binary Tree Level Order Traversal II

    1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vecto ...

随机推荐

  1. golang 无向简单图邻接多重表

    package main import "fmt" type MultipleEdgeNode struct { iVex int iLink *MultipleEdgeNode ...

  2. const与指针的组合

    ① const int *p; //指向一个整型常量的指针,p可变,p指向的对象不可变. ② int const *p; //同上. ③ int * const p; //p不可变,p指向的对象可变( ...

  3. wordpress博客系统

    wordpress博客系统 LNMP:Linux+nginx+mysql+php 一个操作系统+web网站+一个数据库存放数据+后端编程语言 基于红帽操作系统来搭建 1.需要一个本地yum仓库 [ro ...

  4. vue单个插槽

    当子组件模板只有一个没有属性的插槽时,父组件传入的整个内容片段将插入到插槽所在的 DOM 位置,并替换掉插槽标签本身. # 子组件 <div> <h2>我是子组件的标题< ...

  5. vuex中的数据在页面刷新后数据消失

    用sessionstorage 或者 localstorage 存储数据 存储: sessionStorage.setItem( '名', JSON.stringify(值) ) 使用: sessio ...

  6. java redis 短信业务应用

    java redis 短信业务应用 短信业务场景:根据实际业务来通知客户,在短信的MQ中新增字段:是否需要发送短信的标识,短信微服务可以接收到MQ后根据该字段来判断是否发送. 如果发送端MQ的事件的操 ...

  7. discuz论坛个人空间自定义css样式

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` discuz论坛个人空间自定义css样式 日期:2020- ...

  8. 2019徐州区域赛 ACEFM 题解 & pollard-rho & miller-rabin & 求出每个子树的重心 板子

    A. Cat 题目大意:你需要在 [L , R] 选取连续的一段,使得这一段的异或和小于给定的数 S. 请求出最长的长度. 做法:我们可以发现 \[(2k) \oplus (2k+1) = 1, (2 ...

  9. mysql在把子查询结果作为删除表中数据的条件,mysql不允许在子查询的同时删除原表数据

    在上一文中发布了多表删除指定记录,发现达不到我想要的效果,找了很多资料,发现以下方法. 数据库不能边查询边删除, 尝试以下操作 delete from push_msg_overview where ...

  10. 学习嵌入式为什么要学习uboot

    ref:http://www.elecfans.com/d/617674.html 为什么要有BootLoader 背景 很多人学习嵌入式一开始就搞Linux,这样子容易对底层缺少了解. 基础介绍 计 ...