A Magic Lamp

                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
32768/32768 K (Java/Others)

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
 

题意:给出一个不超过1000位的数,求删去m个数字以后形成的最小的数是多少。

分析:我们能够把题目转化为这样一个模型:从A[1]、A[2]、……、A[n] n个数中选出n-m个数,使得组成的数最小。
一、使用RMQ。设原数字长为n,那么除去m个数字后还剩n-m个数字。

(1)由于有n-m个数字。那么在1到m+1位置中最小的那个数字必是结果中的第一个数字,记录其位置为pos

(2)然后从这个位置的下个位置pos+1開始到m+2位置的数字中最小的那个数字必然是结果中第二个数字,以此类推下去向后找。

(3)为了保证数字最小,所以要保证高位最小,还要保证数字长度满足条件
#include<cstdio>
#include<cstring>
#include<cmath> char s[1010];
char ans[1020];
int st[1010][20]; int Min(int x,int y)
{
return s[x] <= s[y] ? x : y;
} void RMQ_Init(int len)
{
for(int i = 0; i < len; i++)
st[i][0] = i;
for(int j = 1; (1<<j) < len; j++)
for(int i = 0; i+(1<<j)-1 < len;i++)
st[i][j] = Min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
} int Query(int l,int r)
{
int k = (int)(log((double)(r-l+1))/log(2.0));
return Min(st[l][k],st[r-(1<<k)+1][k]);
} int main()
{
int len, m, i;
while(scanf("%s%d",s, &m)!=EOF)
{
len = strlen(s);
RMQ_Init(len);
m = len - m;
int pos = 0, num = 0;
while(m--)
{
pos = Query(pos, len - m - 1);
ans[num++] = s[pos++];
}
for(i = 0; i < num; i++)
if(ans[i]!='0')
break;
if(i == num)
printf("0");
else
{
while(i < num)
printf("%c",ans[i++]);
}
puts("");
}
return 0;
}

二、这个题还能够直接使用贪心来求解,思路和RMQ基本同样。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s;
int m;
while(cin >> s >> m) {
int len = s.length(), i, j;
int p = 0, tmp_pos = m, flag = 0;
for(i = 0; i < len - m; i++) {
char mmin = s[p];
for(j = p + 1; j <= tmp_pos; j++)
if(s[j] < mmin) {
mmin = s[j];
p = j;
}
tmp_pos++;
p++;
if(!flag) {
if(mmin == '0') continue;
else {
cout << mmin;
flag = 1;
}
}
else {
cout << mmin;
}
}
if(!flag)
cout << "0";
cout << endl;
}
return 0;
}

最新文章

  1. Isolation-based Anomaly Detection
  2. C#中事件的使用
  3. Mathematics:Pseudoprime numbers(POJ 3641)
  4. jQuery 通配符
  5. &amp; jobs fg Ctrl+z bg
  6. 将List转换成DataTable
  7. JS中的集中页面跳转的方法
  8. where can I find source of com.android.internal.R.styleable.AlertDialog_multiChoiceItemLayout?
  9. deque (STL)
  10. Unity 3D Framework Designing(9)——构建统一的 Repository
  11. Tableau Desktop 10.4.2 的安装和激活
  12. everything of people’s life can changed in their twenties
  13. obj-c编程18:多对多的观察者模式
  14. Linux远程登录ssh免密码配置方法(仅供参考)
  15. python—切片
  16. Java之Logger日志(Java8特性)
  17. final、finally、 finalize 有什么不同
  18. (转)js函数前加分号和感叹号是什么意思?有什么用?
  19. HDU OJ Digital Roots 题目1013
  20. KNY团队与“易校”小程序介绍

热门文章

  1. cocos2d-x在IOS7下面文字显示异常的解决办法 CGBitmapContextCreate: unsupported parameter combination
  2. Centos7中安装Docker
  3. ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法
  4. python程序打包
  5. CSS3实现文字过渡移动
  6. 一张图弄明确开源协议-GPL、BSD、MIT、Mozilla、Apache和LGPL 之间的差别
  7. C#与Java同步加密解密DES算法
  8. 触发JVM进行Full GC的情况及应对策略
  9. Liability
  10. 转:Ogre的MaterialSystem分析