(字符串)2491 移掉K位数字

2491 移掉K位数字

  1. 2 秒
  2.  
  3. 262,144 KB
  4.  
  5. 20 分
  6.  
  7. 3 级题

小b有个正整数num(长度为n),她希望移掉num的k位数字,使得得到的数值最小。

 收起

输入

第一行输入一个正整数n,表示num的位数;
第二行输入一个长度为n的字符串(字符为0-9),表示该n位非负整数num(原num中没有前导0);
第三行输入一个正整数k;
其中0<k≤n<10002

输出

输出一个字符串,表示去掉k位后最小的数

输入样例

7
1432219
3

输出样例

1219
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e4+5;
string str;
int main(){
    int n,k;
    scanf("%d",&n);
    cin>>str;
    scanf("%d",&k);
    if(k==n){
        printf("0\n");
        return 0;
    }
    while(k--){
        int leap=0;
        for(int i=0;i<n-1;i++){
            if(str[i]>str[i+1]){
                leap=1;
                str.erase(i,1);
                break;
            }
        }
        if(!leap)
            str.erase(n-1,1);
        n--;
    }
    //printf("n == %d\n",n);
    int h=n-1;
    for(int i=0;i<n;i++){
        if(str[i]!='0'){
            h=i;
            break;
        }
    }
    for(int i=h;i<n;i++)
        printf("%c",str[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/89067559
今日推荐