58人阅读 评论(0)

# A Magic Lamp

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

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 1
100001 2
12345 2
54321 2

Sample Output
13
1
0
123
321  #include<cstdio>
#include<cstring>
#include<cmath>char s[1010];
char ans[1020];
int st[1010][20];int Min(int x,int y)
{
return s[x] <= s[y] ? x : y;
}void RMQ_Init(int len)
{
for(int i = 0; i < len; i++)
st[i][0] = i;
for(int j = 1; (1<<j) < len; j++)
for(int i = 0; i+(1<<j)-1 < len;i++)
st[i][j] = Min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}int Query(int l,int r)
{
int k = (int)(log((double)(r-l+1))/log(2.0));
return Min(st[l][k],st[r-(1<<k)+1][k]);
}int main()
{
int len, m, i;
while(scanf("%s%d",s, &m)!=EOF)
{
//memset(ans,0,sizeof(ans));
/// memset(s,0,sizeof(s));
// memset(st,0,sizeof(st));
len=strlen(s);
RMQ_Init(len);
int n=len-m;
int pos=0;
int num=0;
while(n--)
{
pos=Query(pos,m++);
ans[num++]=s[pos++];
}
i=0;
while(ans[i]=='0'&&i<num) i++;
if(i==num)
{
printf("0");
}
else
{
for(;i<num;i++)
printf("%c",ans[i]);
}
printf("\n");
}
return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：14255次
• 积分：1417
• 等级：
• 排名：千里之外
• 原创：134篇
• 转载：5篇
• 译文：0篇
• 评论：0条
评论排行