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): 2515 Accepted Submission(s): 983
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
Source
#include<iostream> #include<string.h> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> using namespace std; #define N 110000 #define MOD 100000007 char s[N]; int a[N], m; int Find(int k); void Print(); void Slove(int k); int main() { int n; while(scanf("%s%d", s, &n)!=EOF) { int i, j; m=n; memset(a, 0, sizeof(a)); for(i=0; s[i]; i++) a[i] = s[i]-'0'; i=0; while(1) { int next = Find(i); if(next==0 || next-i>m) break; for(j=i; j<next; j++) { a[j] = -1; m--; } i = next; while(a[i]==0) i++; } Slove(i); Print(); } return 0; } void Slove(int k) { int i, j, index, len=strlen(s)-1; while(1) { if(m==0) break; index=-1; for(i=k; i<=len; i++) { if(a[i]==-1) continue; j = i+1; while(a[j]==-1) j++; if(a[i]>a[j]) { index = i; break; } } if(index==-1) break; a[index] = -1; m--; } } int Find(int k) { int i; for(i=k+1; s[i]; i++) { if(a[i]==0) return i; } return 0; } void Print() { int i, j, flag=0; for(i=0; s[i]; i++) { if(a[i]!=0 && a[i]!=-1) break; } for(j=i; s[j]; j++) { if(a[j]!=-1) { printf("%d", a[j]); flag = 1; } } if(!flag) printf("0"); printf("\n"); }
勿忘初心