题解: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 ...
随机推荐
- 记录一下Android usb相关的知识学习
在SecondStageMain中会先调用PropertyInit做属性初始化,该方法会调用PropertyLoadBootDefaults加载持久化的属性主要加载位置: /system/build. ...
- opensuse tw快速部署
使用GUI快速配置opensusetw 先看官方配置指南 换源 清华源之oss+non-oss links 清华源之packman links sudo zypper ar -cfg 'https:/ ...
- C++笔记(6) 指针
1.指针和数组 指针和数组基本等价的原因在于指针算数和C++内部处理数组的方式.在很多情况下,可以用相同的方式使用数组名和指针名. 在多数情况下,C++将数组名视为数组的第一个元素的地址.指针p的值为 ...
- 异构数据源同步之数据同步 → DataX 使用细节
开心一刻 中午我妈微信给我消息 妈:儿子啊,妈电话欠费了,能帮妈充个话费吗 我:妈,我知道了,我帮你充 当我帮我妈把话费充好,正准备回微信的时候,我妈微信给我发消息了 妈:等会儿子,不用充了,刚刚有个 ...
- WPS WORD EXCEL 不合并显示
WPS WORD EXCEL 不合并显示 版本:WPS 12 , 下载时间约是2023 年. 1.在开始菜单里找到 WPS OFFICE - 配置工具 2.点击"高级(A)". 3 ...
- Ubuntu Server LTS 修改网卡ip地址、固定IP
Ubuntu Server LTS 修改网卡ip地址方式.固定IP. 18.04 之前版本通过修改/etc/network/interfaces 方式,18.04 版本开始通过netplan 方式: ...
- List集合中的元素进行排序
Collections对List集合中的数据进行排序 有时候需要对集合中的元素按照一定的规则进行排序,这就需要用到Java中提供的对集合进行操作的工具类Collections,其中的sort方法 1 ...
- idea设置jdk和设置文件编码格式utf-8
1.idea设置jdk 2.idea设置文件编码格式utf-8 create utf-8 files with NO BOM 不要更改,否则编译会出错误.
- SpringBoot3整合SpringDoc实现在线接口文档
写在前面 在现目前项目开发中,一般都是前后端分离项目.前端小姐姐负责开发前端,苦逼的我们负责后端开发 事实是一个人全干,在这过程中编写接口文档就显得尤为重要了.然而作为一个程序员,最怕的莫过于自己写文 ...
- windows 安装mysql 非常之详细
安装 1.下载安装包 2.解压包 3.文件夹内创建my.ini配置文件,并添加内容 # For advice on how to change settings please see # http: ...