HDU 3183 A Magic Lamp

来源:互联网 发布:域名信息隐藏 编辑:程序博客网 时间:2024/05/19 08:41

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4690    Accepted Submission(s): 1945


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 1100001 212345 254321 2
 

Sample Output
1310123321
 
题目大意:给你一个数字串,让你从中删去n个数字,使剩下的数字组成的数最小

贪心,每次往后面的数字找一遍,如果他的后面有比他小的数,就将其标记

#include<iostream>#include<cstring>#include<cstdio>using namespace std;char str[1010];int num[1010];int main(){//freopen("in.txt","r",stdin);int n;while(~scanf("%s%d",str,&n)){memset(num,0,sizeof(num));int len=strlen(str);for(int i=0;i<len;i++){num[i]=str[i]-'0';}while(n--){for(int i=0;i<len;i++){if(num[i]==-1) continue;int j;for(j=i+1;j<len;j++){if(num[j]>=0){break;}}if(num[i]>num[j]){num[i]=-1;break;}}}int flag=0;for(int i=0;i<len;i++){if(num[i]==-1) continue;if(!flag&&num[i]==0) continue;flag=1;cout<<num[i];}if(!flag) cout<<0;cout<<endl;}}