题目链接: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;
}
}
}