B - Ugu (01字符串非递减)
B-Ugu
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string s_1 s_2 \ldots s_ns1s2…sn. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the following:
- Select an arbitrary index 1 \leq i \leq n1≤i≤n in the string;
- For all j \geq ij≥i, change the value in the jj-th position to the opposite, that is, if s_j = 1sj=1, then make s_j = 0sj=0, and vice versa.
What is the minimum number of operations needed to make the string non-decreasing?
Input
Each test consists of multiple test cases. The first line contains an integer tt (1 \leq t \leq 10^41≤t≤104) — the number of test cases. The description of test cases follows.
The first line of each test cases a single integer nn (1 \leq n \leq 10^51≤n≤105) — the length of the string.
The second line of each test case contains a binary string ss of length nn.
It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅105.
Output
For each test case, output a single integer — the minimum number of operations that are needed to make the string non-decreasing.
Sample 1
Inputcopy | Outputcopy |
---|---|
8 1 1 2 10 3 101 4 1100 5 11001 6 100010 10 0000110000 7 0101010 |
0 1 2 1 2 3 1 5 |
Note
In the first test case, the string is already non-decreasing.
In the second test case, you can select i = 1i=1 and then s = \mathtt{01}s=01.
In the third test case, you can select i = 1i=1 and get s = \mathtt{010}s=010, and then select i = 2i=2. As a result, we get s = \mathtt{001}s=001, that is, a non-decreasing string.
In the sixth test case, you can select i = 5i=5 at the first iteration and get s = \mathtt{100001}s=100001. Then choose i = 2i=2, then s = \mathtt{111110}s=111110. Then we select i = 1i=1, getting the non-decreasing string s = \mathtt{000001}s=000001.
题解:
1.首先我们需要对输出的01字符进行处理,把第一个1前的0全部去除,使得相邻的0合成一个0,相邻的1合成一个1;易得处理后的字符串与原字符串所需操作次数相同
例如:
010010010
操作为:
001101101
000010010
000001101
000000010
000000001
压缩后:
101010
操作为:
010101
001010
000101
000010
000001
操作次数都是五次。
2.这样的压缩操作最方便莫过于栈了,如果当前与栈顶元素相同,则不入栈,反之,入栈。
3.压缩后的字符串规定以0开始,那么可能的组合有 只有 1,01,0三种,其中1只可能出现在字符串的第一位,且操作次数为0。0只可能出现在末尾。
1所需操作次数为 0
01所需操作次数为 2
0所需操作次数为1
4.因此我们需要对1的数量计数,并保存判断最后一个值是 1 还是 0。
若末尾为1:(ct(1)-1)*2
若末尾为0:(ct(1)-1)+1
通过观察又得,所需操作次数就是第一个1(不包括第一个1)后面的字符数量!
代码如下:
#include <iostream>
#include <stack>
using namespace std;
stack<int> s;
int main()
{int t;cin >> t;while (t--){int n;cin >> n;int ct =0;while (!s.empty())//清空栈{s.pop();}char x;while (n--){cin >> x;if (s.empty() || s.top() != x){s.push(x);if (x == '1')ct++;}}if (ct==0)//全为0时,操做次数为0cout << 0 << endl;else{if (s.top() == '1')cout << (ct - 1) * 2 << endl;else{cout << ct * 2 - 1 << endl;;}}}return 0;
}
B - Ugu (01字符串非递减)相关推荐
- 编程 小数位数_使用动态编程的n位数的非递减总数
编程 小数位数 Problem statement: 问题陈述: Given the number of digits n, find the count of total non-decreasin ...
- html标签非成对,深入document.write()与HTML4.01的非成对标签的详解
深入document.write()与HTML4.01的非成对标签的详解 (一)HTML4.01中的非成对标签: 注释标签: 严格来讲不算HTML标签的:文档声明标签 设置页面元信息的:标签 设置网页 ...
- 数据结构_Java_基于 线性表-单链表的初始化、逆序、去重、非递减序列的合并(开辟新链表先整体插入一个链表全部元素,再遍历另外一个链表寻找合适位置插入 、开辟新链表实现舍弃原链表)等操作实现
写在前面 不久前学习了数据结构线性表-数组-链表的相关知识,用C/C++语言实现了 单链表的系列相关操作 .见往期博客: 数据结构实验2_C语言_基于顺序表的非递减有序表的合并.线性表元素的增.删.改 ...
- 干货!基于非递减分位数网络的值分布强化学习及其高效探索方法
点击蓝字 关注我们 AI TIME欢迎每一位AI爱好者的加入! 尽管值分布强化学习在过去几年中得到了广泛的研究,但仍然存在两方面未能解决问题:一是如何保证估计出来的分位数函数的有效性,二是如何有效地利 ...
- 华为OD笔试202010OD笔试华为OD第二题最长的非递减连续子序列的长度
华为OD笔试202010OD笔试华为OD第二题最长的非递减连续子序列的长度要连续的数字序列的最长的长度 直接看输入输出 输入 abc2234019A334bc 输出 4 解释:输入一个字符串,只包含字 ...
- hdu5256序列变换(非递减子序列)
题意(中文直接粘吧) 序列变换 Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个 ...
- 665. 非递减数列
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...
- Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表
代码如下: package sjjgniub;import java.util.LinkedList; import java.util.Scanner;@SuppressWarnings(" ...
- 665. 非递减数列 golang 切片越界问题的探讨(二)
思路 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < ...
最新文章
- linux 修改超级权限密码,linux 修改用户密码
- Boost:以协程的方式实现带有默认值的echo服务器的实例
- VSTS2010部署一:TFS安装
- 在Hibernate中使用存储过程
- 150分试卷c语言,连续5道C语言题目一共送150分啊,题目2.一个农场有头母牛,现 爱问知识人...
- Node.js:package.json中的dependencies和devDependencies区别
- 研究生生存指南之论文
- Maxwell:异构数据源实时同步工具
- word文件做一半未响应_word文档未响应文件还没保存该怎么处理?
- 数据结构(一):数组
- Validity和setCustomVilidity
- vue-H5缩放屏幕以及IOS滚动不流畅
- Iphone开发(7) 太你妈辛苦了
- 1 语言模型和词向量
- 区块链国内外产业发展现状
- 理解LP Simplex
- 大连理工大学计算机组成原理实验,大连理工大学计算机组成原理实验报告(二).docx...
- flutter EventBus
- 如何来做用户意图识别
- java 分割_Java 根据多个连续的符号分割字符串