A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3481 Accepted Submission(s): 1406
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
一下午弄懂一道题,应该是懂了吧。
题意:给你一个字符串s在给你一个数m,让你通过删除s中m个字符使他最小。
思路:一开始看题的时候会误以为每次找出最大的数并把它删除但是并不是那么简单,例如2134 2如果按照上面那个思维我们应该删除3 4结果是21但是正确的结果是13,仔细观察会发现当s[i]>s[i+1]时我们会删除s[i],当删除的个数不到m是我们就把剩下的从后往前删除,因为这时的序列已经按照从小到大的顺序排好当找够m个输出序列就是结果
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
char a[10001];
int m;
int k;
while(~scanf("%s%d",a,&m))
{
int l=strlen(a);
int v[1001],n[1001];
memset(v,0,sizeof(v));
for(int i=0; i<l; i++)
{
n[i]=a[i]-'0';
}
k=0;
int p=m,j;
while(p--)
{
for(int i=0; i<l-1; i++)
{
if(v[i]==0)
{
j=i+1;
while(v[j]==1)//寻找没标记的
j++;
if(n[i]>n[j])
{
v[i]=1;
k++;
break;//找到一个就跳出循环进行下一次寻找
}
}
}
}
int ans=m-k;
for(int i=l-1; i>=0; i--)//如果不足m就倒叙寻找没标记的找到并将其标记
{
if(v[i]==0&&ans>0)
{
v[i]=1;
ans--;
}
}
int tt=0;
for(int i=0; i<l; i++)
{
if(v[i]==0)
{
if(a[i]!='0')
{
tt=1;
}
if(tt==1)
{
printf("%c",a[i]);
}
}
}
if(tt==0)
{
printf("0");
}
printf("\n");
}
return 0;
}