单调栈应用--将一个数删除n各数字之后的最大\最小值

E. Playing with numbers

time limit per test
2.0 s
memory limit per test
64 MB
input
standard input
output
standard output

Folan and Eltan are brothers. However, as all brothers are, they always fight. One day their mom had to go to work, so she decided to give them a task to keep them busy while she is away.

She gave them two numbers S and N.

She told Folan that he has to delete exactly N digits from the number S, so that the resulting number is as small as possible.

Then, she told Eltan that he has to delete exactly N digits from the number S, so that the resulting number is as big as possible.

Folan and Eltan are ex ACMers. They decided to write a program to solve this problem, so they can go back to fighting again.

When their mom heard the evil plan, she decided to make the number S very big, and she may have added leading zeros to it.

The boys were really upset because they couldn't find a way to write a program that would solve this problem fast enough before their mom returns, so they asked for your help.

Can you help them with this program, so they can go back to fighting again?

Input

The first line of the input consists of a single integer t, the number of test cases. Each test case consists of two numbers S and N separated by a single space. where (0 ≤ S < 10100000) and it may contain leading zeros, and (0 ≤ N < |S|).

Note that |S| means the length of the number S.

Output

For each test case, print two lines.

The first line should contain the smallest number Folan can get after deleting exactly N digits from S.

The second line should contain the biggest number Eltan can get after deleting exactly N digits from S.

Please note that in case some of the leading zeros were not deleted, you have to print the resulting number with the remaining leading zeros.

Example
input
Copy
3
00123 2
00123 3
234714812741111111111111111111 4
output
Copy
001
123
00
23
14812741111111111111111111
74812741111111111111111111

题意:给出一个长度最大为100000位的数字,求从中剔除掉N个数字后得到到最大数和最小数。


题解:
若是找最小数,那么维持原来的数字单调递增,若递减,则把前面的数字删去直到递增,遍历一遍后如果没删够,则从后面删。 
若是找最大数,那么维持原来的数字单调递减,若递增,则把前面的数字删去直到递减,便利一遍后如果每删够,则从后面删。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
string s;
string ss;
stack<char>p;
int n,len,cnt,t;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>s;
        cin>>n;
        cnt=n,ss.clear();
        for(int i=0;s[i];i++)
        {
            while(!p.empty()&&s[i]<p.top()&&cnt)//删除之后最小,维持栈单调递增
            {
                p.pop();
                cnt--;
            }
            p.push(s[i]);
        }
        while(cnt--)//遍历完之后还没删够
        {
            p.pop();
        }
        while(!p.empty())
        {
            ss=ss+p.top();
            p.pop();
        }
        len=ss.length();
        for(int i=len-1;i>=0;i--)
            cout<<ss[i];
        cout<<endl;
        cnt=n;
        ss.clear();
        for(int i=0;s[i];i++)//删除之后最大,维持站内单调递减
        {
            while(!p.empty()&&s[i]>p.top()&&cnt)
            {
                p.pop();
                cnt--;
            }
            p.push(s[i]);
        }
        while(cnt--)
        {
            p.pop();
        }
        while(!p.empty())
        {
            ss=ss+p.top();
            p.pop();
        }
        len=ss.length();
        for(int i=len-1;i>=0;i--)
            cout<<ss[i];
        cout<<endl;

    }
}

 

转载于:https://www.cnblogs.com/-citywall123/p/11226501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值