zoukankan      html  css  js  c++  java
  • 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;
    }
    







  • 相关阅读:
    Java 开发环境配置
    JDBC数据批处理
    knockout.js简单实用教程1
    angular 入门教程1
    knockout简单实用教程3
    knockout简单实用教程2
    autofac使用笔记
    MVC WEB api 自动生成文档
    Unity IOC注入详细配置(MVC,WebApi)
    为什么php往mongo里插入整型数字8,变成了numberint(8)
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6935114.html
Copyright © 2011-2022 走看看