Advertisement
clown1337

Untitled

Dec 11th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. void solve() {
  2. string s;
  3. cin >> s;
  4. if (s.size() <= 1) {
  5. cout << "IMPOSSIBLE" << '\n';
  6. return;
  7. }
  8. vector <int> a(26);
  9. for (int i = 0; i < s.size(); ++i) {
  10. a[s[i]-'a']++;
  11. }
  12. string temp;
  13. for (int i = 0; i < s.size(); ++i) {
  14. for (int j = 0; j < 26; ++j) {
  15. if (a[j] && 'a' + j != s[i]) {
  16. temp += 'a' + j;
  17. a[j]--;
  18. break;
  19. }
  20. }
  21. }
  22. if (temp.size() != s.size()) {
  23. char ch;
  24. for (int i = 0; i < 26; ++i) {
  25. if (a[i]) {
  26. ch = 'a'+i;
  27. }
  28. }
  29. if (!temp.size()) {
  30. cout << "IMPOSSIBLE" << '\n';
  31. return;
  32. }
  33. char temp_ch = temp[temp.size()-1];
  34. temp[temp.size()-1] = ch;
  35. temp += temp_ch;
  36. }
  37. if (temp.size() != s.size()) {
  38. cout << "IMPOSSIBLE" << '\n';
  39. return;
  40. }
  41. for (int i = 0; i < s.size(); ++i) {
  42. if (s[i] == temp[i]) {
  43. cout << "IMPOSSIBLE" << '\n';
  44. return;
  45. }
  46. }
  47. cout << s << '\n';
  48. cout << temp << '\n';
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement