Description
给出一个长度为 n n 的数字串,随意删去个位置的数字使得数字最小,输出该最小值
Input
多组用例,每组用例输入一个数字串 s s 和一个不超过的整数 m m ,数字串串长不超过
Output
输出删去 m m 个位置后的数字最小值
Sample Input
178543 4
1000001 1
100001 2
12345 2
54321 2
Sample Output
13
1
0
123
321
Solution
删去后的第一个位(即最高位)必然从中选取,贪心的选取一个最小值(如果有多个最小值贪心的选取高位的值以便留更多的值给后面的位选取),如果第一位位置为 pos1 p o s 1 ,那么第二位必然从 [pos1,n−m−1] [ p o s 1 , n − m − 1 ] 中选取,同理选取高位的最小值,以此类推即可得到最小值,查询区间最小值用 ST S T 表预处理即可
Code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 1111
char s[maxn];
int n,m,st[maxn][22],num[maxn];
int Min(int x,int y)
{
return s[x]<=s[y]?x:y;
}
void ST()
{
for(int i=0;i<n;i++)st[i][0]=i;
for(int j=1;j<=(int)(log((double)n)/log(2.0));j++)
for(int i=0;i+(1<<j)-1<n;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()
{
while(~scanf("%s%d",s,&m))
{
n=strlen(s);
m=n-m;
ST();
int pos=0,res=0;
while(m--)
{
pos=query(pos,n-m-1);
num[res++]=s[pos++]-'0';
}
for(pos=0;pos<res;pos++)
if(num[pos])break;
if(pos==res)
printf("0");
else
for(int i=pos;i<res;i++)
printf("%d",num[i]);
printf("\n");
}
return 0;
}