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 would realize one of her dreams.
The question is:give you a integer, you is allowed to delete exactly m digits. The left digits would form a new integer. You should make it minimum.
You aren't allowed to the order of the digits. Now can-you-help Kiki-realize her dream?
Input
There is several test cases. Each of the test case would contain an integer given (which could at most contains-digits.) and the integer m (if the I Nteger contains n digits, m won't bigger then N). The given integer would not contain leading zero.
Output
For each case, the output of the minimum result 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
Test instructions: gives a string without leading 0 to delete the smallest number after the number of K
Analysis: First determine the number of 0 if there are 0 see 0 before the non 0 number is larger than K if not than the K big priority to remove these characters loop this operation until 0 of the non-0 is larger than K or no 0
If the number of non-0 in the string is not greater than the K output 0 and then is the judgment sequence increment and not increment the case is to remove the next few non-increment words is to remove the previous reverse order number
#include <cstdio> #include <cstring> #include <stack> #include <vector> #include <queue># include<cmath> #include <cstdlib> #include <iostream> #include <algorithm>using namespace std; const int OO = 0x3f3f3f3f;const int maxn = 1e6+7;const int mod = 1e9+7;typedef long long Ll;char str[20000], S[20000];int VIS[30], Sign[20000];int main () {int i, M, K, J, A, ID, CNT, last, G; Last remove 0 before the number of the current position G has how many non 0 number while (scanf ("%s%d", str, &m)!=eof) {g = 0; memset (Vis, 0, sizeof (VIS));///The number of digits in the string is memset (sign, 0, sizeof), or////The character that marks the position is removed for (i = 0; Str[i] ! = ' + '; i++) {vis[str[i]-' 0 ']++; if (str[i]! = ' 0 ') g++; } int len = strlen (str); last = 0; if (m = = Len | | g <= m) {printf ("0\n"); Continue } if (Vis[0] > 0) {while (1)////cycle remove 0 All the numbers that can be removed and 0 {cnt = 0; for (i = last; i < Len; i++) {if (str[i] = = ' 0 ') { id = i; Break } cnt++; } if (cnt > m) break; for (i = last; i < ID; i++) vis[str[i]-' 0 ']--; M-= CNT; while (str[id] = = ' 0 ' && ID < len) {vis[0]--; id++; } last = ID; if (id = = Len | | vis[0] = = 0) break; }}///go unless increment sequence number (reverse number) for (i = last; i < Len; i++) {for (j = i-1; J >=; j--) {if (Str[j] > Str[i] && sign[j] = = 0) {sign[j ] = 1; vis[str[j]-' 0 ']--; m--; } if (M = = 0) break; } if (M = = 0) break; } k = 0; if (M > 0)/////Remove the after m unmarked digits of increment sequence {for (i = len-1; I >= last; i--) {if (Vis [str[i]-' 0 ']) {vis[str[i]-' 0 ']--; m--; } if (M = = 0) break; }//if (M = = 0) break; } for (i = len-1; I >= 0; i--)///ask for the smallest so large numbers should be placed behind {a = str[i]-' 0 '; if (Vis[a] > 0) {s[k++] = a + ' 0 '; vis[a]--; }} k--; The s array is the minimum number after deletion (requires reverse output) while (s[k]== ' 0 ' && k > 0) k--; for (i = k; I >= 0; i--) printf ("%c", S[i]); if (k = =-1) printf ("0"); printf ("\ n"); } return 0;}
The smallest number?