题解: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. 源码分析——MyBatis与Spring整合后如何保证SqlSession线程安全

    在MyBatis架构中SqlSession是提供给外层调用的顶层接口,它是MyBatis对外暴露的最重要的接口,用户通过该接口即可完成数据库的全部操作.在上文中我们明白了我们常用的Mybatis动态代 ...

  2. C# 在Excel中添加、应用或删除筛选器 (日期筛选、文本筛选、数字筛选)

    自动筛选器是 Excel 中的一个基本但极其有用的功能,它可以让你根据特定的条件来自动隐藏和显示你的数据.当有大量的数据需要处理时,这个功能可以帮你快速找到你需要的信息,从未更加有效地分析和处理相关数 ...

  3. NOIP模拟95(多校28)

    T1 嗑瓜子 解题思路 \(f_{i,j}\) 表示操作 \(i\) 次,拿走了 \(j\) 个瓜子的概率,转移就比较直接了: \[f_{i+1,j+1}\leftarrow f_{i,j}\time ...

  4. js 判断闰年

    首先,我们需要了解闰年的判断方式 1.能被4整除 2.并且不能被100整除 3.或者被400整除的 其次我们再来回顾下函数封装的知识,所谓的函数封装就是将一段函数封装成一个工具,有人用到了拿过来就可以 ...

  5. JavaSE 流程控制语句if while for

    目录 控制语句 if条件结构 switch语句 switch与if区别 for 循环 while循环 while与do while 控制语句 条件语句 - 根据不同条件,执行不同语句 if if... ...

  6. Asp .Net Core 系列:详解鉴权(身份验证)以及实现 Cookie、JWT、自定义三种鉴权 (含源码解析)

    什么是鉴权(身份验证)? https://learn.microsoft.com/zh-cn/aspnet/core/security/authentication/?view=aspnetcore- ...

  7. java springboot 指定运行端口

    java springboot 指定运行端口 方法1: 修改源代码里的"\src\main\resources\application.properties" 文件,增加或修改 s ...

  8. 面试官:为什么重写equals方法必须要重新hashCode方法?

    网络上解释的很全面但是很枯涩,也有些难懂,其实就是为了保证当该对象作为key时哈希表的检索效率.如HashMap的get方法是分两步获取的 第一步通过key的哈希值找到对应的哈希桶 第二步通过equa ...

  9. APOLLO DEV环境列表无法显示解决,重启对应的Apollo服务

    APOLLO DEV环境列表无法显示解决,重启对应的Apollo服务 找到项目中的apollo-core jar包中的配置文件,查看dev.meta的服务器配置IP:端口 apollo-env.pro ...

  10. Jx9 虚拟机

    一.Jx9 虚拟机的生命周期 加载 Jx9 脚本 jx9_compile() 或 jx9_compile_file(),加载编译成功后,Jx9 引擎将自动创建一个实例 (jx9_vm) 并且返回指向此 ...