Web Services CodeForces"/>
Azamon Web Services CodeForces
一、内容
题意:问最多替换一对字符后是否能够严格小于另一个串(相等不算作严格小于)
二、思路
- 当a作为c的前缀的时候,a比c小。
- 我们只需要对a进行排序,然后找到与b(排序后的串)第一个不相等字符处,然后将a串这个位置换成b串的这个位置的字符。
- 由于要变成最小的串,所以我们应该从a串的末尾进行查找,这样替换的串更小。
三、代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5005;
string a, b, c;
int t;
int main() {scanf("%d", &t);while (t--) {cin >> a >> c;b = a;sort(b.begin(), b.end());if (b >= c) printf("---\n");else {if (a == b || a < c) cout << a << endl;else {//替换第一个不相同的字符 替换的时候应该尽量选择后面的字符进行替换 for (int i = 0; i < a.size(); i++) {if (a[i] != b[i]) {for (int j = a.size() - 1; j >= 0; j--) {if (a[j] == b[i]) {swap(a[j], a[i]);break;}}break;} } if (a < c) cout << a << endl;else cout << "---" << endl;}} }return 0;
}
更多推荐
Azamon Web Services CodeForces
发布评论