原题目

A Magic Lamp

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

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

********************************************************************************************************************************************************************************************

题意:给你一个数字串 (每一个串有n个数字), 和一个m (要你删除m个数字 ), 使得剩下的数字组成的整数最小。

一开始想了一下删除m个,等价与在数字串中选择 n-m 个数字组成最小整数。

一开始一直做不出,贪心有点问题。后来被dalao教一下就懂了;

贪心的方法:

我们先假设 n 是串的个数, m 是要删除的个数, need 是需要选的个数;

1) 因为是按照顺序组合数字的, 所以第一个删除的数字一定在  [0, n-need] 之内;

证明: 假设数字有 a0, a1, a2, a3, a4, a5 个,要删除2个,n=6, m=2, need = 4;

因为是连续选择的,我们假设在 [0, 3]之内选择第一个,那么我们只剩下a4, a5了,无法构成4个;

所以假设最后的need-1个我们都要, 那么 从 0 ~ (n - 1) - ( need - 1 )==[0, n-need]之内第一个一定在里面(数组从0开始,所以 n-1);

2) 当在[0, n-need]之内选好了第一个,找到第一个的位置pre,那么下一个 数字一定在 [ pre + 1, n-need + 1 ], 就可以一直找下去了,记录你找到的数;

3) 最后在你找到的数里面讨论前导0的情况。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
const int maxn = +;
int d[maxn][maxn];
int a[maxn];
void RMQ_init(string s)
{
memset(d, , sizeof(d));
memset(a, , sizeof(a));
int n = s.size();
for(int i=;i< n ;i++) a[i]=d[i][]=s[i]-'';
for(int j = ; (<<j) <= n ;j++)
for(int i = ; i+(<<j ) - < n ;i++)
d[i][j] = min(d[i][j-] , d[i+(<<(j-))][j-] );
}
int RMQ(int L, int R)
{
int k=;
while((<<(k+)) <= R-L+) k++;
return min(d[L][k], d[R-(<<k) + ][k]);
}
int findmin(int L, int R, int Min)
{
for(int i=L; i <= R ;i++){
if(a[i] == Min) return i;
}
}
int main()
{
string s;
int m, n, need;
while(cin >> s >> m){
RMQ_init(s);
n = s.size() ;
need = n - m;
int Left=, Right = n - need;
// cout << Left << " " << Right << endl;
// cout << RMQ(Left, Right) << endl;
deque <int > q;
while(){
if(need == ) break;
int Min = RMQ(Left, Right);
// cout << Min << endl;
q.push_back(Min);
int pre = findmin(Left, Right, Min);
Left = pre+;
Right ++ ;
need --;
}
while(!q.empty()){
if(q.front() == ) q.pop_front();
else break;
}
if(q.empty()){
cout << "";
}
else{
while(!q.empty()){
cout << q.front();
q.pop_front();
}
}
cout << endl;
}
return ;
}

最新文章

  1. October 18th, Week 43rd Tuesday, 2016
  2. Android自定义PopupWindow显示在控件上方或者下方
  3. 以Debug模式启动JBoss
  4. hadoop中NameNode、DataNode和Client三者之间协作关系及通信方式介绍
  5. Easyui-Combobox多选下拉框
  6. jdk与cglib的动态代理
  7. C++豆知识索引
  8. SqlServer之like、charindex、patindex(转载)
  9. 求知成瘾+逻辑成瘾+博识的无知,你中枪没?我感觉中枪了 - 外野 - Stage1st - Powered by Discuz!
  10. 更多文章请关注公众号:FullStackPlan 或前往个人主页:www.linbingdong.com
  11. 阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试
  12. index.html jquery
  13. haar的简单应用(2)
  14. pageisELIgnored作用
  15. gaea-editor 知识点
  16. (01背包 第k优解) Bone Collector II(hdu 2639)
  17. 04:第一个OC类
  18. 《Web性能权威指南》
  19. time和datetime
  20. CS中窗体的基类(BaseForm)注意点

热门文章

  1. Mac008--快捷键
  2. Mac003--Maven安装与环境变量配置
  3. 001---mysql分库分表
  4. python学习笔记:__init__.py的作用
  5. Spring Security 01
  6. [LuoguP1829]Crash的文明表格(二次扫描与换根+第二类斯特林数)
  7. C. Trailing Loves (or L&#39;oeufs?) (质因数分解)
  8. 任务21 :了解ASP.NET Core 依赖注入,看这篇就够了
  9. Java中POJO及其细分XO、DAO的概念
  10. 攻防世界--maze