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?
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.
If the result contains leading zero, ignore it.
178543 4 1000001 1 100001 2 12345 2 54321 2
13 1 0 123 321
#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;
}