题解:CF1971D Binary Cut
题解:CF1971D Binary Cut
题意
给予你一个 \(01\) 字符串,你可以将它分割,分割后必须排成先 \(0\) 后 \(1\) 的格式。
求最少分割为几部分。
思路
将 \(0\) 和 \(1\) 分离出来。
如:
010001 -> 0 1 000 1
求出一共有几部分,记为 \(sum\)。
接下来分类讨论:
- \(sum=1\),即只有 \(1\) 或 \(0\) 的序列,输出 \(1\)。
- \(sum=2\) 且开头为 \(1\),即需分成两段的序列,所以输出 \(2\)。
- 其它情况,输出 \(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的更多相关文章
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode(67)题解: Add Binary
https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a bin ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- LeetCode题解之Binary Tree Pruning
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...
- LeetCode题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
- LeetCode题解之Binary Tree Paths
1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root ...
- LeetCode题解之Binary Tree Level Order Traversal II
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vecto ...
随机推荐
- 面向编程对象的好处及应用紧耦合VS松耦合(继承,多态)(1-2)
面向编程对象的好处及应用紧耦合VS松耦合(继承,多态)(1-2) 当初: 代码是做了客户端与业务的分离的封装 现在: 加深下功底,在上一个随笔之前做一个修改和拓展(继承,多态) 作业: 现在从计算器变 ...
- Understanding Swift’s value type thread safety - 代码分析(一)
结构体并不代表线程安全,swift在此上未做保证 func testScenarioA() throws { var store: Int = 0 DispatchQueue.concurrentPe ...
- K-D Tree 总结
Luogu题单 前置芝士 \(K-D\;Tree\) 例题略解 P2479 [SDOI2010]捉迷藏 大概就是 K-D Tree 的板子题了吧,网上的打法都不太友好,参考了 fengwu 的打法. ...
- k8s集群创建阿里云版本
阿里云创建k8s集群实例 创建两个8G内存的抢占实例(青岛),能ssh 默认关闭防火墙,且没有交换分区 配置/etc/hosts,主机名 配置k8s仓库 配置命令补全 安装docker(需要conta ...
- js数组操作——对象数组根据某个相同的字段分组
先说点废话 最近在实际业务中,需要编写一个方法根据数组中每一个对象的一个相同字段,来将该字段值相等的对象重新编入一个数组,返回一个嵌套的数组对象,特地来做个总结. 当然需要注意的是,在开发过程这种数组 ...
- P1737
problem \(\text{task 1}\) 要求: 输入:\(a,b\). 输出:\(-2a-2b\). 数据范围:\(|a|,|b| \le 10^9\). 做法: 先把 \(-2\) 提出 ...
- 铭瑄 USB 供电不足
铭瑄 USB 供电不足 可能是USB固件开了节能,节能状态和某些设备会不兼容,更新固件试试固件链接:链接:https://pan.baidu.com/s/1RxHEddYe6TWMDlMJ3PQB1Q ...
- koishi机器docker搭建
硬件要求: 可用内存:1G以上 存储空间:1G以上 cpu:不限制 配置: 在docker的存储空间目录建立koishi文件夹 下载docker镜像 koishijs/koishi 建立容器,具体设置 ...
- python-API开发zk客户端
前面于超老师讲完了,zk运维的基本命令行玩法,更多的还是开发需要通过代码和zk结合处理. 大多数场景是java后端去操作. 这里我们以运维更友好的python来学习. 1.kazoo模块 zookee ...
- output打印ElasticSearch搜索条件searchSourceBuilder对象 toString方法
打印搜索条件:log.info(searchSourceBuilder.toString());log.info("es搜索条件:[{}]", searchSourceBuilde ...