NYOJ 寻找最大数

寻找最大数

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大,

比如当n=92081346718538,m=10时,则新的最大数是9888

 

输入
第一行输入一个正整数T,表示有T组测试数据
每组测试数据占一行,每行有两个数n,m(n可能是一个很大的整数,但其位数不超过100位,并且保证数据首位非0,m小于整数n的位数)
输出
每组测试数据的输出占一行,输出剩余的数字按原次序组成的最大新数
样例输入
2
92081346718538 10
1008908 5
样例输出
9888
98
我突然不知道怎么来表达了
我们将字符串的位序表示下 0 1 2 3 4 5 6 7 8 9 10 11 12 。。。。。假如我们要删除m个数字 使得最大,我们可以在序列里面【0-m】中找一个最大的t(位序)
然后从这个【t+1,m+1]开始找 只需要找len-m次
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=100000+10;
17 char str[N];
18 int vis[N];
19 map<char,int>mp;
20 int main(){
21     int t;
22     scanf("%d",&t);
23     while(t--){
24         memset(vis,0,sizeof(vis));
25         mp.clear();
26         int m;
27         scanf("%s",str);
28         scanf("%d",&m);
29         int ans=0;
30         int len=strlen(str);
31         int t=0;int tt=m;
32         for(int k=0;k<len-m;k++){
33             char c='0';
34             for(int i=t;i<=tt;i++){
35                 if(str[i]>c){
36                     t=i;
37                     c=str[i];
38                 }
39             }
40             vis[t]=1;
41             t++;
42             tt++;
43         }
44         for(int i=0;i<len;i++){
45             if(vis[i]==1)cout<<str[i];
46         }
47         cout<<endl;
48     }
49 }

 



posted on 2017-02-28 23:35  见字如面  阅读(260)  评论(0编辑  收藏  举报

导航