71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
/*
|
||
abacc
|
||
|
||
答案:
|
||
aabcc
|
||
|
||
<<<<<<< HEAD
|
||
|
||
测试用例2:
|
||
ab
|
||
|
||
参考答案:
|
||
ba
|
||
=======
|
||
abccc
|
||
abcde
|
||
>>>>>>> 4448591d2a10bd325d874361e0ff815a5b53237f
|
||
*/
|
||
const int N = 5e5 + 10;
|
||
typedef pair<int, int> PII; // 一维:最小值,二维:位置
|
||
PII small[N]; // i及i后面的最小字符
|
||
int main() {
|
||
string s;
|
||
cin >> s;
|
||
PII t = {200, 0}; // a:97,z=a+25=122
|
||
// 倒序枚举
|
||
/**
|
||
adccc
|
||
|
||
acccd
|
||
*/
|
||
for (int i = s.size() - 1; i >= 0; i--) {
|
||
if (s[i] < t.first) { // 如果游标里装的最小字符大于当前字符,那么当前字符将成为游标中的最小字符
|
||
t.first = s[i]; // 最小字符
|
||
t.second = i; // 最小字符位置
|
||
}
|
||
small[i] = t;
|
||
}
|
||
|
||
string s1 = s;
|
||
// 正序枚举
|
||
for (int i = 0; i < s.size(); i++)
|
||
if (small[i].first < s[i]) {
|
||
swap(s[i], s[small[i].second]);
|
||
break;
|
||
}
|
||
<<<<<<< HEAD
|
||
|
||
if (s1 == s) {
|
||
// 如果替换完还是同一个字符串,那么需要替换最后两个不一样的字符,这是题目的要求
|
||
char x = s[s.size() - 1];
|
||
for (int i = s.size() - 2; i >= 0; i--) {
|
||
if (s[i] != x) {
|
||
swap(s[s.size() - 1], s[i]);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
=======
|
||
if (s1 == s) {
|
||
swap(s[s.size() - 1], s[s.size() - 2]);
|
||
}
|
||
>>>>>>> 4448591d2a10bd325d874361e0ff815a5b53237f
|
||
// 输出答案
|
||
cout << s << endl;
|
||
return 0;
|
||
} |