http://acm.hdu.edu.cn/showproblem.php?pid=3183
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2556 Accepted Submission(s): 999
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
123
321
题目大意:给一个数,删除其中的m位是其值变得最小,求删除后最小值
看了样例之后,以为只要删除m个较大的数,就看可以了, 结果一直wa,后来发现 21435 1这组数据,
按照这个思路删掉最大的一个数5得到结果2143,发现这不是最小的结果,1435才是最小的结果,这种思路是不对的,
那么要让结果最小,那么如果高位数字比低位数字大的话,就删除这个高位数字,这样就让结果尽可能的小了
比如这个例子:
235564 3
要删除m = 3个数,发现2<3,3<5,5<=5,5<6,高位都不大于低位,而6>4,这时删去6(m = 2),23554, 发现5>4,删去5(m = 1)2354,
发现5>4,删去5(m = 0)234,最终结果为234,这样循环遍历,具体看代码
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; typedef long long ll;
const int N = ; int main()
{
char s[N];
int a[N], str[N];
int m, i;
while(~scanf("%s%d", s, &m))
{
memset(str, , sizeof(str));
int k = , l = strlen(s);
for(i = ; s[i] != '\0' ; i++)
a[i] = s[i] - '';
for(i = ; i < l && m > ; i++)
{
if(a[i] <= a[i + ])
str[k++] = a[i];
else
{
m--;
while(str[k - ] > a[i + ] && m > && k >= )
{
m--;
k--;
}
}
}
for(; i < l ; i++)
str[k++] = a[i];
for(i = ; i < k - m ; i++)
if(str[i] != )
break;
if(i == k - m)
printf("");
else
{
for(int j = i ; j < k - m ; j++)
printf("%d", str[j]);
}
printf("\n");
}
return ;
}