题解: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. Android 13 - Media框架(1)- 总览

    关注公众号免费阅读全文,进入音视频开发技术分享群! 为了加深对Android Media框架的理解,决定在这里记录下学习过程中碰到的问题以及一些个人思考.由于本人水平有限,笔记的内容可能会有一些错误, ...

  2. ceph deploy部署ceph集群 ceph扩容 rbd存储

    架构拓扑 节点主机 节点IP 节点角色 OS ceph-admin 10.0.0.60 admin deploy mds centos7 ceph-node1 10.0.0.61 mon osd md ...

  3. C++笔记(7)成员初始化列表

    成员初始化列表由逗号分隔的初始化列表组成(前面带冒号),它位于参数列调的右括号之后,函数体左括号之前.如果数据成员的名称为mdata,并需要将它初始化为val,则初始化器为mdata(val). Cl ...

  4. itest(爱测试)开源接口测试&敏捷测试&极简项目管理 8.0.0 发布,测试重大升级

    (一)itest 简介及更新说明 itest 开源敏捷测试管理,testOps 践行者,极简的任务管理,测试管理,缺陷管理,测试环境管理,接口测试,接口Mock 6合1,又有丰富的统计分析.可按测试包 ...

  5. Qt初始化代码基本说明

    参考视频:黑马程序员https://www.bilibili.com/video/BV1XW411x7NU?p=4 1  工程基本介绍 按照这个基本步骤(https://www.cnblogs.com ...

  6. Nodejs中间件 中间件分类和自定义中间件

    中间件 中间件理解 中间件可以理解为业务流程的中间处理环节.如生活中吃一般炒青菜,大约分为如下几步骤 express中当一个请求到达的服务器之后,可以在给客户响应之前连续调用多个中间件,来对本次请求和 ...

  7. THUWC 2024 游记

    其实这个游记也没啥好写的-- day 0 上午做动车两个小时到重庆,路上玩了 1.5h 的星露谷. 下午去巴蜀中学报道试机,系统是 Ubuntu Jammy,大大的好评,只是桌面是 Xubuntu/X ...

  8. 关于 CSDN 的恶臭嘴脸

    我有段时间确实希望通过 CSDN 增加我文章的阅读量.但是我怎么使用 CSDN,我想要的大概也就 cnblogs 相对符合预期,CSDN 真是垃圾. 为了钱真是什么都不要了,让我们这种没有自主经济来源 ...

  9. 剑指Offer-64.滑动窗口的最大值(C++/Java)

    题目: 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6, ...

  10. 接口自动化之request模块

    1.安装 方式一.命令行直接 pip install requests 方式二.PyCharm中,File >> Settings >> Project:Practice &g ...