前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces】1230B - Ania and Minimizing

【Codeforces】1230B - Ania and Minimizing

作者头像
喜欢ctrl的cxk
发布2019-11-08 12:05:37
5740
发布2019-11-08 12:05:37
举报
文章被收录于专栏:Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/101830422

Problem Description:

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 Specification:

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 SS has exactly n digits and doesn't contain any leading zeroes.

Output Specification:

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

Sample Input1:

代码语言:javascript
复制
5 3
51528

Sample Output1:

代码语言:javascript
复制
10028

Sample Input2:

代码语言:javascript
复制
3 2
102

Sample Output2:

代码语言:javascript
复制
100

Sample Input3:

代码语言:javascript
复制
1 1
1

Sample Output3:

代码语言:javascript
复制
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位数str,允许改变str上的k位,求能构成的最小的数是多少。分情况讨论:①若k=0说明str的一位数都不能改变,直接将str进行输出;②若n=1说明str只有个位数,直接将str置零进行输出;③n不为1且k不为0,先将str的第一位数置为1,然后再将剩下的位数全部置为0即可。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n,k;    //str的长度为n,允许改变str的k位数
    string str;
    cin >> n >> k >> str;
    if(!k)    //k=0说明str的一位都不能改变
    {
        cout << str << endl;
    }
    else if(n == 1)    //str只有个位数且k不为0则直接将str置零
    {
        cout << 0 << endl;
    }
    else
    {
        if(str[0] != '1')    //第一位数置1
        {
            str[0] = '1';
            k--;
        }
        Up(i,1,n-1)    //剩下的位全部置0
        {
            if(str[i]!='0' && k)
            {
                str[i] = '0';
                k--;
            }
        }
        cout << str << endl;
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/10/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input1:
  • Sample Output1:
  • Sample Input2:
  • Sample Output2:
  • Sample Input3:
  • Sample Output3:
  • Note:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档