Files
python/TangDou/CSP-J/J2_2020/tangdou.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

71 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}