cf-1281b-Azamon Web Services

题目链接:cf-1281b

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 ss and 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.

题目大意:给出 n 行 s,t 字符串。s字符串中的任意两字符可以交换,最多一次。(也可以选择不交换),然后输出小于 t 的 s字符串,如果不能满足条件输出 “—” .(不带引号).

想法本来想的是,一直匹配,s[now]与t[now], 直到s[now]>t[now]时,从now后的,字符中找出小于t[now]的字符. 直接对s进行排序,得到s的最小字典序组合s_now,然后让s与s_now进行匹配,得到交换一次的新的s字符串,然后直接与t比较。

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		string s,t;
		string ss;
		cin>>s>>t;
		
		
		if(s>=t){
			ss=s;
			sort(ss.begin(),ss.end());
			char nows;
			int index=0,index1=0;
			for(int i=0;i<s.length();i++){
				if(s[i]!=ss[i]){  //得到 只交换一次的最小的s字符
					index=i;  // 需要交换字符的位置1
					nows=ss[i];
					break;
				}
			}
			for(int i=index+1;i<s.length();i++){
				if(s[i]==nows){
					index1=i; // 需要交换字符的位置2
				}
			}
		//	printf("%d      %d  \n",index,index1);
			swap(s[index],s[index1]);
		}
		if(s>=t){
			printf("---\n");
		}else{
			cout<<s<<endl;
		}
	}	
	
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值