码迷,mamicode.com
首页 > 其他好文 > 详细

FZU Problem 1853 Number Deletion

时间:2015-08-12 09:00:14      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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;
}







版权声明:本文为博主原创文章,未经博主允许不得转载。

FZU Problem 1853 Number Deletion

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/47438685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!