hdu3183——A Magic Lamp(RMQ)

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
大意:给你一个数字串,要你删除m个数字后所得的最小数字串是多少。 思路:转换思想选出n-m个使其最小,178543选2个使其最小,第1个数从1-4中选,选1,第2个数从上个数+1位置开始选,即从7-3中开始选,选3,答案13
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1008
#define LL long long
using namespace std;
int dp[N][30];
char s[N];int s1[N];
void RMQ(int n)
{
    int i,j;
    for(j=1; 1<<j<=n; j++)
        for(i=0; i+(1<<j)-1<n; i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int main()
{
    int m,i,a,b;
    while(~scanf("%s%d",s,&m))
    {
        int cat=0;
        int len=strlen(s);
        if(m>len)
        {
            puts("0");
            continue;
        }
        else if(m<=0)
        {
            puts(s);
            continue;
        }
        for(i=0; i<len; i++)
            dp[i][0]=s[i]-'0';
        RMQ(len);
        m=len-m;//选m个数
        a=0,b=len-m;从[a,b]区间选
        //LL sum=0;长度1000可能超限,所以用字符数组储存
        while(m--)
        {
            int k=log(b-a+1.0)/log(2.0);
            int num=min(dp[a][k],dp[b-(1<<k)+1][k]);
            s1[cat++]=num;
            for(i=a; i<=b; i++)
            {
                if(s[i]==num+'0')
                    break;
            }
            a=i+1,b++;
        }
        for(i=0; i<cat; i++)
        {
            if(s1[i]!=0)
            {
                break;
            }
        }
        if(i==cat)
        {
            puts("0");
            continue;
        }
        for(i; i<cat; i++)
            printf("%d",s1[i]);
        cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值