hdu3183——A Magic Lamp(RMQ)

来源:互联网 发布:java随机读取数组的值 编辑:程序博客网 时间:2024/06/05 03:50
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 1100001 212345 254321 2
Sample Output
1310123321
大意:给你一个数字串,要你删除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 longusing 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;}