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







  • 相关阅读:
    Hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
    Vijos 1456 最小总代价 (状压dp)
    洛谷 P1313 计算系数 (二项式定理)
    洛谷 P1134 阶乘问题
    EINTR错误
    TCP和UDP协议的应用/参数查看
    BAT面经
    高级环境编程要看的
    UDP丢包和无序 问题的解决方法
    tcp/ip
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6935114.html
Copyright © 2020-2023  润新知