删数问题

给你一个N位数,从中去掉K个数字,能得到的最大的数是多少?

INPUT

有T测试数据,每组测试数据第一行由N和K2个整数组成(1 ≤ K < N ≤ 500 000),第二行是N位数(非0开头)。

OUTPUT

对每组数据输出去掉K个数字得到的最大数。

SAMPLE TESTS

IN

OUT

3

4 2

1924

7 3

1231234

10 4

4177252841

94

3234

775841

 

 

贪心思想:

      从头遍历k次,每次遇到不递减的数字,就将其删除掉,并结束该次循环。每次循环都如上所做。

       其中如果遇到从头到尾一直都是递减的,则删除最后一个数字。

 

代码实现:

 

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main(){
     int m;
     cin>>m;
     while(m--){
        int n1,n2;
        scanf("%d%d",&n1,&n2);
       // cin>>n1>>n2;
        string a;
        cin>>a;
        for(int i=0;i<n2;i++){
                int j;
                string::iterator it;       //指针 
                it=a.begin();
                int temp;
                for(j=0;j<n1-1;j++){
                         temp=n1;
                        if(a[j]<a[j+1]){
                              a.erase(it);   //string中的删字符函数 
                              n1--;
                              break;
                              }
                        it++;
                              }
                if(j>=temp-1){
                     a.erase(it);
                     n1--;
                     }
                              }
        cout<<a<<endl;
        }
     return 0;
     }
    

 

 

posted on 2012-08-19 19:40  yumao  阅读(181)  评论(0)    收藏  举报

导航