FZU Problem 1853 Number Deletion

Problem 1853 Number Deletion

Accept: 80    Submit: 239
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Given you one n-digital positive integer a,After removing any of them k( k < n) digits, the remaining figures form a new positive integer according to the origin order. For a given n-digital positive integers a and positive integer k. Now ask you to find a algorithm to minimize the remaining integer.

 Input

The first line contains one integer t, represents the number of test cases.

Then following t lines,each line contain two numbers a,k (0 < a < 10^1000, k is less than the length of a).

 Output

Output the mininum number after the deletion.(the number can not contain leading zero)"> it means that we should ignore the leading zeros of the output number

 Sample Input

1178543 4

 Sample Output

13

题意:在一个数中,删除k个数,使得剩下的数最小。

思路:让高数位尽量小。于是就要从前向后扫,每次就删当前数前面,比自己大的数;

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
int vis[2222];
int T;
char s[2222];
int k;

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%d",s,&k);
        int i=0,j=1;
        int len=strlen(s);
        memset(vis,0,sizeof vis);

        while(k && j<len)//保证留在前面的数尽量小。于是要删除每一个数前面比他大的
        {
            for(int t=i;t>=0;t--)//为什么从近到远删,697982  697543 
            {
                if(!vis[t] && s[t]>s[j])
                {
                    vis[t]=1;
                    k--;
                }
                if(k==0)break;
            }
            i=j;
            j++;
        }

        for(i=len-1;i>=0 && k;i--)//一遍扫完之后发现还没删到k个数。那么就要从后向前删。由于前面已经是最小的数字了,保证了从小到大的排列
        {
            if(vis[i]==0)
            {
                vis[i]=1;
                k--;
            }
            if(k==0) break;
        }

        int flag=0;
        for(int i=0;i<len;i++)
        {
            if(vis[i])continue;
            if(flag || s[i]!='0')
            {
                printf("%c",s[i]);
                flag=1;
            }
        }
        if(flag==0) puts("0");//假设所有删完了,那么就要输出0
        else
        puts("");

    }

    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值