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
rmp问题,首先是要删除 del 个元素原长是 len 剩下的长度是len-del,要保证最后剩下的数字最小,就是第一位是前del+1位的最小的数字,这个最小的数字下标是pos,然后第二位数字就是从pos+1位开始到前del+2 位的最小值; (ps:注意输入输出的问题就可以)
代码;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
char ans[1005];
char num[1005];
int dp[1005][1005];
int Min(int i,int j)
{
return num[i]<=num[j] ? i:j;
}
void rmp_st(int n)
{
int i,j;
for(i=0;i<n;i++)
dp[i][0]=i;
for(j=1;j<=(int)(log((double)n)/log(2.0));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 rmp_find(int L,int R)
{
int x=(int)(log(double(R-L+1))/log(2.0));
return Min(dp[L][x],dp[R-(1<<x)+1][x]);
}
int main()
{
//freopen("in.txt","r",stdin);
int del;
while(~scanf("%s%d",num,&del))
{
int len=strlen(num);
rmp_st(len); del = len - del;
int pos = 0; int sum = 0;
while ( del-- )
{
pos = rmp_find(pos,len-del-1);
ans[sum++] = num[pos++];
}
int flag = 0;
for ( int i = 0; i < sum; i++ )
{
if ( ans[i] != '0' ) flag = 1;
if ( flag ) printf("%c",ans[i]);
}
if ( flag == 0 ) printf("0");
printf("\n");
}
}