hdu 3183(RMQ应用)

来源:互联网 发布:tv650网络电视成人 编辑:程序博客网 时间:2024/06/05 08:50

A Magic Lamp

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


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 1100001 212345 254321 2
 

Sample Output
1310123321
 

Source
HDU 2009-11 Programming Contest
 

Recommend
lcy
 
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3183
分析:这题可以通过变形为取n个数,使大小最小变成RMQ问题,没什么好说的。。。
这题一开始wa了无数次,原以为是ST算法写错,经过各种Debug,发现没有在ST()这个函数里重新开变量i,j,导致wa。。。所以函数里的变量还是尽量不要用全局变量了,特别是ST算法这种易错的东西。。。
代码:
#include<cstdio>#include<cstring>#define check(i,j) (a[i]<=a[j]?i:j)using namespace std;const int mm=1111;int f[mm][11];char a[mm],out[mm];int i,j,k,n,m;void ST(){    int i,j;    for(i=0;i<n;++i)f[i][0]=i;    for(j=1;(k=(1<<j)-1)<n;++j)        for(i=0;i+k<n;++i)            f[i][j]=check(f[i][j-1],f[i+(1<<(j-1))][j-1]);}int get(int l,int r){    int k=0;    while(l+(1<<k)<r-(1<<k)+1)++k;    return check(f[l][k],f[r-(1<<k)+1][k]);}int main(){    while(scanf("%s %d",a,&m)!=-1)    {        n=strlen(a);        m=n-m;        ST();        i=j=0;        while(m--)i=get(i,n-m-1),out[j++]=a[i++];        for(i=0;i<j;++i)            if(out[i]!=48)break;        if(i==j)putchar('0');        while(i<j)putchar(out[i++]);        puts("");    }    return 0;}


原创粉丝点击