Swap Digits

Description

Now we have a number, you can swap any two adjacent digits of it, but you can not swap more than K times. Then, what is the largest probable number that we can get after your swapping?

Input

There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.

For each test case, there is an integer K (0 <= K < 106) in the first line, which has the same meaning as above. And the number is in the next line. It has at most 1000 digits, and will not start with 0.

There are at most 10 test cases that satisfy the number of digits is larger than 100.

Output

For each test case, you should print the largest probable number that we can get after your swapping.

Sample Input

3
2
1234
4
1234
1
4321

Sample Output

3124
4213
4321

Hint


Submit Page

#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int T,k;
	string str;
	cin>>T;
	while(T--)
	{
		int temp=0;
		cin>>k>>str;
		while(k)
		{
			int Max=str[temp]-'0',flag=temp;
			int pos=temp+k>str.length() ? str.length()-1 : temp+k;
			for(int i=temp+1;i<=pos;i++)
			{
				if(str[i]-'0'>Max) Max=str[i]-'0',flag=i;
			}
			k-=flag-temp;
			//cout<<flag<<endl;
			
			int ff=str[flag]-'0';
			for(int i=flag-1;i>=temp;i--)				
				str[i+1]=str[i];				
			str[temp]=ff+'0';
			temp++;
			if(temp>=str.length()) break;
		//	cout<<k<<endl;
		}
		cout<<str<<endl;
	}
	
	return 0;
}
/**********************************************************************
	Problem: 1270
	User: song_hai_lei
	Language: C++
	Result: AC
	Time:12 ms
	Memory:2180 kb
**********************************************************************/



猜你喜欢

转载自blog.csdn.net/song_hai_lei/article/details/80444724