分析:
给出一个长度最大为100000位的数字,求从中剔除掉N个数字后得到到最大数和最小数。题解:
非常简单的一道贪心:
若是找最小数,那么维持原来的数字单调递增,若递减,则把前面的数字删去直到递增,遍历一遍后如果没删够,则从后面删。
若是找最大数,那么维持原来的数字单调递减,若递增,则把前面的数字删去直到递减,便利一遍后如果每删够,则从后面删。AC代码:
/*************************************************************************
> File Name: E.cpp
> Author: Akira
> Mail: qaq.febr2.qaq@gmail.com
> Created Time: 2016年08月15日 星期一 15时43分48秒
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;
#define MaxN 100000
#define MaxM MaxN*10
#define INF 1000000000
#define bug cout<<88888888<<endl;
int T,N;
char S[112345];
char MIN[112345];
char MAX[112345];
void output(char x[],int ll)
{
for(int i=ll-1;i>=0;i--)
{
cout << x[i];
}
cout << endl;
}
int main()
{
scanf("%d", &T);
while(T--)
{
string S;
int N;
cin >> S;
scanf("%d", &N);
stack<char> Stack;
int K = N;
for (int i = 0; i < S.length(); i++)
{
while (!Stack.empty() && K > 0 && S[i] < Stack.top())
{
Stack.pop();
K--;
}
Stack.push(S[i]);
}
while(K--) Stack.pop();
int cnt = 0;
while(!Stack.empty())
{
MIN[cnt++] = Stack.top();
Stack.pop();
}
K = N;
for(int i = 0;i<S.length();i++)
{
while( !Stack.empty() && K>0 && S[i] > Stack.top() )
{
Stack.pop();
K--;
}
Stack.push(S[i]);
}
while(K--) Stack.pop();
cnt = 0;
while(!Stack.empty())
{
MAX[cnt++] = Stack.top();
Stack.pop();
}
output(MIN, S.length()-N);
output(MAX, S.length()-N);
}
return 0;
}