Codeforces Round #607 (Div. 2) 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.


贪心题

只要记录每个字母出现的位置,然后从小到大排好序后,按照小的字母排前面的原则,如果发现一个字母的位置出现在一个比它大的字母后面,那么交换这两个字母的位置,一次就行;注意,这里交换的必须是那个更小字母的最后面一个出现位置;

还要注意一些细节,就是AAAA和AAAA不符合,AAA和AAAAA符合;

代码:

#include<bits/stdc++.h>
using namespace std;
vector<int>ve[30];
int main(){
	int t;
	cin>>t;
	while(t--){
		string s1,s2;
		cin>>s1>>s2;
		for(int i=0;i<30;i++) ve[i].clear();
		for(int i=0;i<s1.length();i++){
			ve[s1[i]-'A'].push_back(i);
		}
		for(int i=0;i<26;i++){
			if(ve[i].size()) sort(ve[i].begin(),ve[i].end());
		}
		int ans=0;
		int c=-1;
		int ok=0;
		for(int i=0;i<26;i++){
			if(ok) break;
			for(int j=0;j<ve[i].size();j++){
				if(ve[i][j]!=ans){
					c=ve[i][ve[i].size()-1];
					ok=1;
					break;
				}
				ans++;
			}
		}
		if(c!=-1){
			char t;
			t=s1[c];
			s1[c]=s1[ans];
			s1[ans]=t;
		}
		int flag=1;
		for(int i=0;i<s1.length();i++){
			if(i>=s2.length()){
				flag=0;
				break;
			}
			if(s1[i]==s2[i]) continue;
			if(s1[i]<s2[i]){
				flag=1;
				break;
			}
			else{
				flag=0;
				break;
			}
		}
		if(s1==s2) flag=0;
		if(flag){
			cout<<s1<<endl;
		}
		else{
			cout<<"---"<<endl;
		}
	} 
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值