B. Azamon Web Services

半世苍凉 提交于 2020-01-31 22:47:34

链接:https://codeforces.com/contest/1281/problem/B

Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.

After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!

To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.

Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!

Given the string ss representing Jeff's product name and the string cc representing his competitor's product name, find a way to swap at most one pair of characters in ss (that is, find two distinct indices ii and jj and swap sisi and sjsj) such that the resulting new name becomes strictly lexicographically smaller than cc, or determine that it is impossible.

Note: String aa is strictly lexicographically smaller than string bb if and only if one of the following holds:

  • aa is a proper prefix of bb, that is, aa is a prefix of bb such that a≠ba≠b;
  • There exists an integer 1≤i≤min(|a|,|b|)1≤i≤min(|a|,|b|) such that ai<biai<bi and aj=bjaj=bj for 1≤j<i1≤j<i.

Input

The first line of input contains a single integer tt (1≤t≤15001≤t≤1500) denoting the number of test cases. The next lines contain descriptions of the test cases.

Each test case consists of a single line containing two space-separated strings ss and cc (2≤|s|≤5000,1≤|c|≤50002≤|s|≤5000,1≤|c|≤5000). The strings ssand cc consists of uppercase English letters.

It is guaranteed that the sum of |s||s| in the input is at most 50005000 and the sum of the |c||c| in the input is at most 50005000.

Output

For each test case, output a single line containing a single string, which is either

  • the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than cc. In case there are many possible such strings, you can output any of them;
  • three dashes (the string "---" without quotes) if it is impossible.

Example

input

Copy

3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA

output

Copy

AMAZON
---
APPLE

Note

In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".

It is impossible to improve the product's name in the second test case and satisfy all conditions.

In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".

代码:

#include<bits/stdc++.h>
using namespace std;
string a,b;
long long t,n;
int main() 
{
	cin>>t;
	while(t--) 
	{
		cin>>a>>b;
		n=a.size();
		int mi=-1,las=-1,lmi=-1;
		for(int i=n-1;i>=0;i--) 
		{
			if(~mi&&a[mi]<a[i])
			las=i,lmi=mi;
			else if(!~mi||a[i]<a[mi])
			mi=i;
		}
		if(~las)
		swap(a[las],a[lmi]);
		if(a<b)
		cout<<a<<endl;
		else 
		cout<<"---"<<endl;
	}
	return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!