题解: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. Windows 7 任务栏开发 之 进度条(Progress Bar)

    上一篇我们完成了"覆盖图标"(Overlay Icon)的相关开发,本篇我们将对进度条特性进行研究.在使用IE 下载文件时,任务栏图标会同步显示当前下载进度(如下图).那么在应用程 ...

  2. 自动化测试在 Kubernetes Operator 开发中的应用:以 OpenTelemetry 为例

    背景 最近在给 opentelemetry-operator提交一个标签选择器的功能时,因为当时修改的函数是私有的,无法添加单测函数,所以社区建议我补充一个 e2e test. 因为在当前的版本下,只 ...

  3. 使用rem、动态vh自适应移动端

    前言 这是我的 模仿抖音 系列文章的第六篇 第一篇:200行代码实现类似Swiper.js的轮播组件 第二篇:实现抖音 "视频无限滑动"效果 第三篇:Vue 路由使用介绍以及添加转 ...

  4. kubernetes命令补全k8s命令补全

    echo "source <(kubectl completion bash)" >> ~/.bashrc source <(kubectl complet ...

  5. .NET周刊【6月第1期 2024-06-02】

    国内文章 一文带你了解.NET能做什么? https://www.cnblogs.com/Can-daydayup/p/18214473 .NET是一个免费.开源.跨平台的开发平台框架,广泛应用于桌面 ...

  6. css作用 文本属性 颜色属性

    Css作用:美化页面 各种css属性 接触的 css属性 width: height: background: Color 选择符和声明 声明包括 属性:属性值 选择符:{ 属性:属性值 } 1:文本 ...

  7. java中判断一个String字符串或字符数组中包含某个字段

    /** * 判断String字符串中包含某个字段 * @param oriStr 原始字符串 * @return */ private static boolean findString(String ...

  8. Caffe样例中mnist的文件之间逻辑分析

    约定一下,Caffe运行样例时在终端中使用的所有命令,同时终端中的目录已经cd到Caffe之中(别告诉我一个Github项目你还没有make all就直接拿过来用了) sudo sh data/mni ...

  9. Golang支持重试的http客户端ghttp

    简介 官方仓库:https://github.com/GuoFlight/ghttp 重试的逻辑依赖了github.com/avast/retry-go 入门 client := ghttp.Clie ...

  10. Linux设备模型:6、Bus

    作者:wowo 发布于:2014-4-15 19:21 分类:统一设备模型 原创文章,转发请注明出处.蜗窝科技,www.wowotech.net. 概述 在Linux设备模型中,Bus(总线)是一类特 ...