HDU 3183 贪心

A Magic Lamp

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


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 1
100001 2
12345 2
54321 2
 
Sample Output
13
1
0
123
321
 
Source
 
题意:给你一个长度最多为1000的数 删去m位 使得最后的数最小 存在前导零
题解:贪心 处理 从左到右遍历 找到第一个a[i]>a[i+1] 并删除第i个  vector 处理
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<stack>
 5 #include<map>
 6 #include<cstring>
 7 #include<cstdio>
 8 #include<vector>
 9 #define ll __int64
10 using namespace std;
11 char a[1005];
12 vector<int> ve;
13 int flag;
14 int n;
15 int main()
16 { 
17  memset(a,0,sizeof(a));
18   while(cin>>a>>n)
19 {
20     int len;
21     len=strlen(a);
22     if(len<=n)
23     cout<<"0"<<endl;
24     else
25     {
26         ve.clear();
27       for(int i=0;i<len;i++)
28           ve.push_back(a[i]-'0');
29         while(n--)
30         {
31             int flag=1;
32             for(int i=0;i<ve.size()-1;i++)
33             {
34                 if(ve[i]>ve[i+1])
35                 {
36                     flag=0;
37                     ve.erase(ve.begin()+i);
38                     break;
39                 }
40             }
41             if(flag)
42             {
43                 ve.erase(ve.end()-1);
44             }
45         }
46         flag=1;
47         for(int i=0;i<ve.size();i++)
48         {
49             if(ve[i]==0&&flag)
50               continue;
51             else
52             {
53                 flag=0;
54                 cout<<ve[i];
55             }
56         }
57         if(flag)
58         cout<<"0";
59         cout<<endl;
60     }
61     memset(a,0,sizeof(a));
62 }    
63     return 0;
64 } 
原文地址:https://www.cnblogs.com/hsd-/p/5397328.html