需要先填一遍字母,保证集合一样,使用map来匹配比较简单
#include <iostream> #include <queue> #include <map> #include <set> #include <cmath> #include <cstring> #include <algorithm> #include <iomanip> #include <stack> #include <numeric> #include <ctime> #include <string> #include <bitset> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; const ll N = 1e6 + 5, mod = 1e9 + 7, inf = 0x3f3f3f3f; int n; void solve() { string a; cin >> a; string temp = a; sort(temp.begin(), temp.end()); if (temp[0] == temp[temp.size() - 1]) { cout << -1 << '\n'; return ; } map<char, int>mp; for (auto i : a) { mp[i] = 2; } int num = mp.size(); for (auto i : a) { bool flag = false; for (auto &j : mp) { if (num) { if (j.first != i && j.second == 2) { cout << j.first; j.second = 1; flag = true; num--; break; } } else { if (j.first != i) { cout << j.first; flag = true; break; } } } if (!flag) { for (auto j : mp) { if (j.first != i) { cout << j.first; break; } } } } cout << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; //cin>>t; while (t--) { solve(); } return 0; }