You are given a binary string of length n (i. e. a string consisting of n characters ‘0’ and ‘1’).

In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than k moves? It is possible that you do not perform any moves at all.

Note that you can swap the same pair of adjacent characters with indices i and i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.

You have to answer q independent test cases.

Input
The first line of the input contains one integer q (1≤q≤104) — the number of test cases.

The first line of the test case contains two integers n and k (1≤n≤106,1≤k≤n2) — the length of the string and the number of moves you can perform.

The second line of the test case contains one string consisting of n characters ‘0’ and ‘1’.

It is guaranteed that the sum of n over all test cases does not exceed 106 (∑n≤106).

Output
For each test case, print the answer on it: the lexicographically minimum possible string of length n you can obtain from the given one if you can perform no more than k moves.

Example
Input
3
8 5
11011010
7 9
1111100
7 11
1111100
Output
01011110
0101111
0011111
Note
In the first example, you can change the string as follows: 110−−11010→10−−111010→011110−−10→01110−−110→0110−−1110→01011110.

In the third example, there are enough operations to make the string sorted.
思路:思路挺好想的一道题,但是一不留心就出错。。
在规定的移动次数内,我们先在前面找出一个1,然后再其后面找出一个0.然后(尽可能的)交换位置。具体看代码
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;string s;
int n;
ll k;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%lld",&n,&k);cin>>s;int l=0,r;while(l<n&&s[l]=='0') l++;r=l+1;while(r<n&&s[r]=='1') r++;//起始位置一定要提前找出来,否则会出错或者超时。while(k){while(l<n&&s[l]=='0') l++;while(r<n&&s[r]=='1') r++;if(l>=n||r>=n) break;if(k>=(r-l)){swap(s[l],s[r]);k-=(ll)(r-l);l++,r++;}else{swap(s[r-k],s[r]);//这就是尽可能的交换。k=0;}}cout<<s<<endl;}return 0;
}

努力加油a啊,(o)/~

Binary String Minimizing CodeForces - 1256D(贪心)相关推荐

  1. Binary String Reconstruction CodeForces - 1352F(思维+构造)

    For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...

  2. D1. Kirk and a Binary String (easy version)

    题目链接:http://codeforces.com/contest/1204/problem/D1 D1. Kirk and a Binary String (easy version) time ...

  3. 题解 CF1399D 【Binary String To Subsequences】

    题目链接:http://codeforces.com/contest/1399/problem/D 题目描述: You are given a binary string s consisting o ...

  4. Juju and Binary String(前缀和)

    Juju and Binary String [Link](Problem - F - Codeforces) 题意 给你一个010101串strstrstr长度为nnn,这个串的权定为onesn(o ...

  5. 【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)

    题干: 链接:https://ac.nowcoder.com/acm/contest/883/B 来源:牛客网 ZYB loves binary strings (strings that only ...

  6. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alpha ...

  7. Binary String Matching

    Binary String Matching 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your t ...

  8. Binary String Constructing(CF-1003B)

    Problem Description You are given three integers aa, bb and xx. Your task is to construct a binary s ...

  9. 2019牛客暑期多校训练营(第三场) B-Crazy Binary String

    题目描述 ZYB喜欢二进制字符串(只包含'0'和'1'的字符串).他喜欢equal binary string(其中字符串中的"0"数和"1"数相等) ZYB想 ...

最新文章

  1. 如何提高企业竞争力,科学的组织架构是第一王牌
  2. 自动生成Insert数据的SQL脚本
  3. 获取一个窗口的所有子窗口(包括嵌套) - 回复 asian 的问题
  4. new子类会先运行父类的构造函数
  5. 能识别nvme的pe启动_PE系统纯净(可以识别nvme固态)
  6. 拜托!面试请不要再问我 Spring Cloud 底层原理 ...
  7. Solr 部分 局部字段修改 更新 删除
  8. [uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)
  9. CSS3 矢量图标及背景精灵
  10. 罗永浩望着 7 年前的罗永浩
  11. 时间序列分析和预测(含实例及代码)
  12. 数据包络分析DEA有哪些指标?
  13. error: undefined reference to __write_chk错误
  14. hp笔记本win10无法关闭飞行模式
  15. 紫光展锐发布系统级安全的高性能5G SoC移动平台T820
  16. 阿里巴巴著名的“管理三板斧”
  17. 【100%通过率】华为OD机试真题 Java 实现【处理器问题/ 高性能AI处理器】【2022.11 Q4 新题】
  18. Tolua for Unity3d 编译字节码
  19. google的RateLimiter限流器的使用
  20. C语言循环结构输出爱心,C语言--循环结构(示例代码)

热门文章

  1. sonarqube没有html插件,SonarQube Github插件没有写拉问题的问题
  2. python PIL 打开\显示\保存图像
  3. VIDEOIO ERROR: V4L: can't open camera by index 0
  4. jeecgboot 弹出modal刷新主窗体_保利天汇|自然的温度,刷新园林美学天际/辽宁房产特刊...
  5. 前端面试题目汇总摘录(JS 基础篇)
  6. mysql 在windows中安装问题
  7. 关于might_sleep的一点说明---CONFIG_DEBUG_ATOMIC_SLEEP【转】
  8. Commvault发布V11版数据管理平台 优化数据资源
  9. Mongodb查询语句与Sql语句对比
  10. 3DMax的OFusion插件的使用问题