B. Azamon Web Services---思维

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 s representing Jeff’s product name and the string c representing his competitor’s product name, find a way to swap at most one pair of characters in s (that is, find two distinct indices i and j and swap si and sj) such that the resulting new name becomes strictly lexicographically smaller than c, or determine that it is impossible.

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

a is a proper prefix of b, that is, a is a prefix of b such that a≠b;
There exists an integer 1≤i≤min(|a|,|b|) such that ai<bi and aj=bj for 1≤j<i.
Input
The first line of input contains a single integer t (1≤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 s and c (2≤|s|≤5000,1≤|c|≤5000). The strings s and c consists of uppercase English letters.

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

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 c. 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
inputCopy

3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
outputCopy
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”.

题意:给你两个串,让你把第一个串只交换一对字符,使得小于第二个串

解析:给a串排序,排序后的串为b,然后两个同时匹配,如果不相同那就在原串中找和a相同的那个字符,然后与交换。
这样会是最优的,因为排序之后是最优的,只要交换一组使与排序数组相同一段。


#include<bits/stdc++.h>
using namespace std;
int t;
string s1,s2;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s1>>s2;
		if(s1<s2)
		{
			cout<<s1<<endl;
			continue;
		}
		string b=s1;
		sort(b.begin(),b.end());
		for(int i=0;i<s1.size();i++)
		{
			int f=0;	
			if(b[i]!=s1[i])
			{
				for(int j=s1.size()-1;j>=0;j--)
				{
					if(s1[j]==b[i])
					{
						f=1;
						swap(s1[i],s1[j]);
						break;
					}
				}
			}
			if(f==1) break;
		}
		if(s1<s2) cout<<s1<<endl;
		else cout<<"---"<<endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值