A Magic Lamp

原题如下

A Magic Lamp

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
1010101 1

Sample Output
13
1
0
123
321
10101

题意:
给定一串数字,取走m个元素,使得剩下的数字按原来顺序组合在一起得到的数字最小,输入是同时输入组。运用S-T法处理,S-T法详见上一篇博客。

此题最大的难点
1.对0的处理。前导0忽略,但后续0不能忽略。我在OUTPUT函数中使用了一个叫做NND的变量解决了这个问题。
2.如果只有前导0,显然不能不输出,所以当结果为0 时要能输出0,我用了变量mmp来解决这个问题。
3.字符串转化为数组,不能直接转换,要减48,这个事情是经过调试之后才发现的,与scII码有关。

看代码需要注意的地方
这篇代码是在上一篇博客有关RMQ-S-T的代码改编来的,如出一辙,如有不懂之处,可以参考上一篇博客的说明。也可以留言。

代码

#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;//若要用队列,则此行必不可少

int min(int a,int b)
{
    return a>=b?b:a;
}

void S_T(int*Array,int N,int(*Dp)[20]) //S-T法预处理
{
    for(int i=1;i<=N;i++) Dp[i][0]=Array[i];
    for(int j=1;(1<<j)<=N;j++)
        for(int i=1;i+(1<<j)<=N+1;i++)
        Dp[i][j]=min(Dp[i][j-1],Dp[i+(1<<(j-1))][j-1]);
}

int MAX_RMQ(int(*Dp)[20],int P,int Q) //根据预处理后的Dp数组求[P,Q]区间的最大值
{
    int k=0,dis=Q-P+1;
    while((1<<(k+1))<=dis) k++;
    return min(Dp[P][k],Dp[Q-(1<<k)+1][k]);
}

void OUTPUT(int(*Dp)[20],int *Array,int N,int M)  //应该是这道题的核心了,非零则输出,没有输出则输出0,这个地方v最容易出错
{
    int MMP=0,pos=1,endd=M+1,goupi=1,NND=0;
    for(int kase=1;kase<=N-M;kase++)
    {
        goupi=MAX_RMQ(Dp,pos,endd);
        if(goupi) {NND++;printf("%d",goupi);MMP++;}
        else if(NND)
            {
                printf("%d",goupi);MMP++;
            }
        for(int j=pos;j<=endd;j++)
            {
                if(Array[j]==goupi) {pos=j+1;break;}//break千万不要写成continue,否则会继续小循环
            }
        endd++;
    }
    if(!MMP) printf("0");
    printf("\n");
}

int main()
{
    char import[1010];//把输入的字符串变成数组Array,从Array[1]开始放数字
    int key,N=0,M=0;
    while(scanf("%s%d",import,&M)!=EOF)
    {
       N=strlen(import);
       int*Array=(int*)malloc(sizeof(int)*(N+1));
       for(int i=1;i<=N;i++) Array[i]=import[i-1]-48;//这里居然要减去48,坑
       int (*Dp)[20]=(int(*)[20])malloc(sizeof(int)*(N+1)*20);//分配一个(N+1)*11的二维数组
       S_T(Array,N,Dp);
       OUTPUT(Dp,Array,N,M);
    }

    return 0;
}

en, jiu shi zhe yang zi la~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值