题解: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 12(S) MultiMedia Learning(一)开篇

    这个系列将会作为自己学习android多媒体的笔记,如果有错误请帮忙指正. 本系列的学习均基于Android 12(S),代码来源:http://aospxref.com/

  2. 批量处理图片(resize;grayscale)以及重命名

    批量处理图片(resize:grayscale)以及重命名 做深度学习,有大量图片需要做为训练集,为方便批量resize,rename,灰度化,制作python脚本. 先纪录一些函数: resize ...

  3. 搭建内网yum仓库

    服务器配置: 1. 创建yum仓库的目录,rpm包上传到这里. mkdir /soft/x86 -p 2. 修改yum缓存配置: sed -i 's#keepcache=0#keepcache=1#g ...

  4. iOS符号表手工还原

    1.通过Xcode的Device工具导出app.crash文件 2.将.crash 和 .dSYM符号 app放在同一个目录中 3.寻找symbolicatecrash,将symbolicatecra ...

  5. 一种基于E3处理器平台的NAS完整方案(从电脑组装到网站部署)

    一种基于E3处理器平台的NAS完整方案(从电脑组装到网站部署) 本文将简要简要介绍本人自建NAS的完整配置,截至发文此NAS已经连续良好运行一年,应当说具有良好的稳定性. 本文所述配置包含洋垃圾成分, ...

  6. itestwork(爱测试)开源一站式接口测试&敏捷测试工作站 9.0.0 GA 发布,重大升级

    (一)itest 简介 itest work (爱测试)  一站式工作站让测试变得简单.敏捷.itest work 包含极简的任务管理,测试管理,缺陷管理,测试环境管理,接口测试,接口Mock 6合1 ...

  7. SQL必知必会(第5版) 读书笔记

    适用范围 本书涵盖的DBMS一般来说,本书中所讲授的SQL可以应用到任何数据库管理系统(DBMS).但是,各种SQL实现不尽相同,本书介绍的SQL主要适用于以下系统(需要时会给出特殊说明和注释): ❑ ...

  8. python实现取得成员所在的多个位置

    注:本代码主要是为了实现多个集合之间求并集时的辅助代码,简单的举个例子来说明代码的功能. 约定:例如{11: [2, 3]}表示数据11在集合2和集合3中都存在. 现有以下数据: d0 = {38:  ...

  9. 地址栏hash模式以?问号分割也可以分割的

    可以看到href里面hash没有? 但是还是以?分割了 就很不明白 但是我就indexof判断有没有? 再进行下一步逻辑 这里记录一下坑

  10. EF 开始的片段时有问题 具有潜在运行时冲突

    错误 3002: 映射从第 149 行开始的片段时有问题:表 t_Apply  的键(t_Appl .Id)具有潜在运行时冲突: 列(t_Apply .Id)映射到概念端 EntitySet t_Ap ...