CodeForces Ania and Minimizing 1230B

大家觉得写还可以,可以点赞、收藏、关注一下吧!
也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn


CodeForces Ania and Minimizing 1230B 👻

Problem

Ania has a large integer S. Its decimal representation has length n and doesn’t contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S still won’t contain any leading zeroes and it’ll be minimal possible. What integer will Ania finish with?

Input

The first line contains two integers n and k (1≤n≤200000, 0≤k≤n) — the number of digits in the decimal representation of S and the maximum allowed number of changed digits.

The second line contains the integer S. It’s guaranteed that S has exactly n digits and doesn’t contain any leading zeroes.

Output

Output the minimal possible value of S which Ania can end with. Note that the resulting integer should also have n digits.

Examples

inputCopy
5 3
51528
outputCopy
10028
inputCopy
3 2
102
outputCopy
100
inputCopy
1 1
1
outputCopy
0

Note

A number has leading zeroes if it consists of at least two digits and its first digit is 0. For example, numbers 00, 00069 and 0101 have leading zeroes, while 0, 3000 and 1010 don’t have leading zeroes.

分析

题目意思就是一个包含n个字符的数字,最多改变k次数字,使得数字变得最小

  1. 开头如果为1,则不改变
  2. 中间如果不为0,则不改变
  3. 如果数位为1,且k>0,则直接为0

代码


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        char[] c = sc.next().toCharArray();
        if (n == 1 & k > 0)
            System.out.println(0);
        else if (k != 0) {
            if (c[0] != '1') {
                k--;
                c[0] = '1';
            }
            for (int i = 1; i < n; i++) {
                if (k == 0)
                    break;
                if (c[i] != '0') {
                    k--;
                    c[i] = '0';
                }
            }
            for (int i = 0; i < n; i++)
                System.out.print(c[i]);
        } else {
            for (int i = 0; i < n; i++)
                System.out.print(c[i]);
        }
    }
}

2020年9月15日更

大家觉得写还可以,可以点赞、收藏、关注一下吧!
也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值