A Magic Lamp(HDU - 3183)

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? 

InputThere 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. 
OutputFor 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

题意,从一个长度为N的字符串中删除M个字符,使剩下的字符串最小。

从N长字符串中删除M个字符,相当于从N长字符串中挑N-M个字符组成最小字符串,那么第一个字符应该是从[0,M]中找最小字符,假如它的下标为K,则第二个寻找范围为[k+1,M+1],
依此类推,直到找完,用ST表模拟即可。

#include<iostream>
#include<cstdio>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1001;
int d[maxn][10];
char a[maxn],num[maxn];
int n, m;
int minn(int i,int j)//核心
{
    return a[i]<=a[j]?i:j;
}
int getminn(int l,int r)
{
    int k=log2(r-l+1);
    return minn(d[l][k],d[r-(1<<k)+1][k]);
}
int main()
{
    while(scanf("%s%d",a,&m)!=EOF)
    {
        int len=strlen(a);
        n=len;
        m=len-m;
        for(int i=1;i<n;i++)  //下标的序列
            d[i][0]=i;
        for(int j=1;(1<<j)<= n;j++)
            for(int i=0;i+(1<<j)-1< n;i++)
                d[i][j]=minn(d[i][j-1], d[i+(1<<(j-1))][j-1]);//返回代表值小的下标值. 
        int i,j;
        i=j=0;
        while(m--)
        {
            //int l=i;
            //int r=len-m-1;
             i=getminn(i,len-m-1);  //while中m已经--了所以例如第一次就相当于0到m去找第一位
               //int k=log2(r-l+1);
               //i==minn(d[l][k],d[r-(1<<k)+1][k]);
            num[j++]=a[i++];
        }
        for(i=0;i<j;i++)
        if(num[i]!='0')
        break;
        if(i==j)
        {
            printf("0\n");
            continue;
        }
        while(i<j)
        {
            printf("%c",num[i]);
            i++;
        }
        printf("\n");
    }
}
View Code
 
 

 

 

转载于:https://www.cnblogs.com/switch-waht/p/11405550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值