A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1010 Accepted Submission(s): 378
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?
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.
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.
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
Source
Recommend
lcy
第一次接触这个算法,刚开始看的时候没能一下在看懂,后来明白了之后发现其实这个算法还是比较好理解的,同时也觉得这个算法比较神奇
这个算法是要求有一个预处理过程,然后查询的速度就快了!
算法是一用了DP的思想,然后建立一个状态方程初始化
状态方程是DP[i][j]=MIN(DP[i][j-1],DP[i+(1<<(j-1))][j-1])
上面的动态方程可以这样理解,dp[i][j] 可以分成从 i->i+2^(j-1) 和 i+2^(j-1)->i+2^(j-1)+2^(j-1)
后面两个都求出来了,因此前面的只要取后面两者较小的一个就OK 了!
DP[i][j]表示的意思是从i开始后2^j个元素的最小值
下面就是查询,其实上面的还比较好理解,下面查询我觉得是RMQ算法的关键
在查询的时候是线性的,因为一个区间长度,那么分成两份,这两份组合起来一定是整个区间,分别求两边,得出的结果取较小的一个!
不懂看看代码就懂了!
int query(int l,int r)
{
int k=log(double(r-l+1))/log(2.0);
if(str[dp[l][k]] <= str[dp[r-(1<<k)+1][k]])
return dp[l][k];
else
return dp[r-(1<<k)+1][k];
}
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[1500];
char ans[1500];
int dp[1500][20];
#define MIN(a,b) (a>b?b:a)
int init(int n)
{
int i,j;
for(i=0;i<n;i++)
dp[i][0]=i;
for(j=1;(1<<j)<n;j++)
for(i=0;i+(1<<j)-1<n;i++)
{
if(str[dp[i][j-1]]<=str[dp[i+(1<<(j-1))][j-1]])
dp[i][j]=dp[i][j-1];
else
dp[i][j]=dp[i+(1<<(j-1))][j-1];
}
/*
*上面的动态方程可以这样理解,dp[i][j] 可以分成从 i->i+2^(j-1) 和 i+2^(j-1)->i+2^(j-1)+2^(j-1)
*后面两个都求出来了,因此前面的只要取后面两者较小的一个就OK 了!
*/
return 0;
}
int query(int l,int r)
{
int k=log(double(r-l+1))/log(2.0);
if(str[dp[l][k]] <= str[dp[r-(1<<k)+1][k]])
return dp[l][k];
else
return dp[r-(1<<k)+1][k];
}
int main()
{
int n;
int i,j,len;
int m,pos;
int k;
while(scanf("%s%d",str,&n)!=EOF)
{
len=strlen(str);
init(len);
pos=0;
k=0;
m=len-n;
while(m--)
{
pos=query(pos,len-m-1);
ans[k++]=str[pos++];
}
for(i=0;i<k;i++)
if(ans[i] !='0')
break;
if(i==k)
printf("0\n");
else
{
for(i;i<k;i++)
printf("%c",ans[i]);
printf("\n");
}
}
return 0;
}