Beautiful No 【DFS 暴力】

Anton has a positive integer n, however, it quite looks like a mess, so he wants to make it beautiful after k swaps of digits.

Let the decimal representation of n as (x1x2… xm)10 satisfying that 1 ≤ x1 ≤ 9, 0 ≤ xi ≤ 9 (2 ≤ i ≤ m), which means . In each swap, Anton can select two digits xi and xj (1 ≤ i ≤ j ≤ m) and then swap them if the integer after this swap has no leading zero.

Could you please tell him the minimum integer and the maximum integer he can obtain after k swaps?

Input

The first line contains one integer T, indicating the number of test cases.

Each of the following T lines describes a test case and contains two space-separated integers n and k.

1 ≤ T ≤ 100, 1 ≤ n, k ≤ 109.

Output

For each test case, print in one line the minimum integer and the maximum integer which are separated by one space.

Example

Input

5
12 1
213 2
998244353 1
998244353 2
998244353 3

Output

12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332

Tips

给一个数,求你k次交换能得到的最大数和最小数,用深搜求全排列加减枝

#include<bits/stdc++.h>
#define read() freopen("input.txt","r",stdin);
#define write() freopen("output.txt","w",stdout);
void FILE_IO(){
	#ifndef ONLINE_JUDGE
	read();write();
	#endif
}
using namespace std;
string mini,maxi,line;
void bt(int pos,int taken){
	if(pos>=line.size()){
		cout<<line<<'\n';
		maxi=max(maxi,line);
		if(line[0]!='0')
		mini=min(mini,line);
		return;
	}
	bt(pos+1,taken);
	if(taken-1>=0){
		for( int i=pos+1; i<line.size(); i++ ){
			swap(line[i],line[pos]);
			bt(pos+1,taken-1);
			swap(line[i],line[pos]);
		}
	}
}
int main() {
	FILE_IO();
	read();
	int t,k;
	cin>>t;
	while(t--){
		cin>>line>>k;
		mini=line;
		maxi=line;
		bt(0,k);
		cout<<mini<<' '<<maxi<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43323172/article/details/89606296
今日推荐