A Magic Lamp -- hdu -- 3183

本文解析了一道名为“Magic Lamp”的ACM编程题,题目要求从给定整数中删除指定数量的数字以形成最小可能的新整数。文章提供了详细的解题思路与完整的代码实现。

 

http://acm.hdu.edu.cn/showproblem.php?pid=3183

 

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2515    Accepted Submission(s): 983


Problem Description
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams. 
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
 

 

Input
There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
 

 

Output
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it. 
 

 

Sample Input
178543 4
1000001 1
100001 2
12345 2
54321 2
 

 

Sample Output
13
1
0
123
321
 

 

Source

 

 

 

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;

#define N 110000
#define MOD 100000007

char s[N];
int a[N], m;

int Find(int k);

void Print();

void Slove(int k);

int main()
{
    int n;

    while(scanf("%s%d", s, &n)!=EOF)
    {
        int i, j;

        m=n;

        memset(a, 0, sizeof(a));
        for(i=0; s[i]; i++)
            a[i] = s[i]-'0';

        i=0;
        while(1)
        {
            int next = Find(i);
            if(next==0 || next-i>m)
                break;

            for(j=i; j<next; j++)
            {
                a[j] = -1;
                m--;
            }


            i = next;
            while(a[i]==0) i++;
        }

        Slove(i);

        Print();
    }
    return 0;
}

void Slove(int k)
{
    int i, j, index, len=strlen(s)-1;

    while(1)
    {
        if(m==0) break;

        index=-1;
        for(i=k; i<=len; i++)
        {
            if(a[i]==-1) continue;
            j = i+1;
            while(a[j]==-1) j++;

            if(a[i]>a[j])
            {
                index = i;
                break;
            }
        }
        if(index==-1)
            break;

        a[index] = -1;
        m--;
    }
}

int Find(int k)
{
    int i;

    for(i=k+1; s[i]; i++)
    {
        if(a[i]==0)
            return i;
    }
    return 0;
}

void Print()
{
    int i, j, flag=0;

    for(i=0; s[i]; i++)
    {
        if(a[i]!=0 && a[i]!=-1)
            break;
    }
    for(j=i; s[j]; j++)
    {
        if(a[j]!=-1)
        {
             printf("%d", a[j]);
             flag = 1;
        }
    }
    if(!flag) printf("0");

    printf("\n");
}

 

转载于:https://www.cnblogs.com/YY56/p/4965013.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值