传送门:HDU 3183
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
题意:
给出一个数n 再给一个数m 问 在n中拿掉m个数后,n最小是多少
n用字符串来存 最多1000位
题解:
要我们求去掉m个数后最小是多少,我们可以反向思考,将题目转化为在n中选(n-m)个数,能组成的最小的数。
所以我们从第m+1个数开始往前找,找最小的数(可以是0),当然找的数越往前越好,这样可以给后面的开辟空间。将这个位置标记下来,下次搜到这个位置时直接break;这个数就是我们最小数的第一个数。
下次从m+2这个位置开始搜,方法同上。如此往复搜n-m次,确定最小数。
然后对这个最小的数去前缀0即可。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
const int N=11000;
char s[N];
int a[N],vis[N];
int m;
int main()
{
while(~scanf("%s",s))
{
int cnt=0;
memset(vis,0,sizeof(vis));
scanf("%d",&m);
int len=strlen(s);
for(int i=m;i<len;i++)
{
int minn=10,k=i;
for(int j=i;j>=0;j--)
{
if(vis[j]) break;
if(s[j]-'0'<=minn)
{
minn=s[j]-'0';
k=j;
}
}
vis[k]=1;
a[cnt++]=s[k]-'0';
}
int x=0;
while(a[x]==0&&x<cnt)//去0
{
x++;
}
if(x==cnt) printf("0\n");//全是0的情况,输出0
else
{
for(int i=x;i<cnt;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
}
return 0;
}