Advertisement
jasonpogi1669

Think Like a Coder: Scrambled Words Problem using C++

May 21st, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios::sync_with_stdio(false);
  7.     cin.tie(0);
  8.     string s;
  9.     getline(cin, s);
  10.     for (int i = 0; i < (int) s.size(); i++) {
  11.         if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) {
  12.             int decipher = s[i] - 3;
  13.             if (s[i] >= 'a' && s[i] <= 'c') {
  14.                 int diff = 'a' - decipher - 1;
  15.                 s[i] = (char) ('z' - diff);
  16.             } else if (s[i] >= 'A' && s[i] <= 'C') {
  17.                 int diff = 'A' - decipher - 1;
  18.                 s[i] = (char) ('Z' - diff);
  19.             } else {
  20.                 s[i] = (char) (decipher);
  21.             }
  22.         }
  23.     }
  24.     vector<bool> visited((int) s.size(), false);
  25.     for (int i = 0; i < (int) s.size() - 1; i++) {
  26.         for (int j = i + 1; j < (int) s.size(); j++) {
  27.             if (s[i] >= 'a' && s[i] <= 'z') {
  28.                 if ((s[j] < 'a' || s[j] > 'z') && (s[j] < 'A' || s[j] > 'Z')) {
  29.                     break;
  30.                 }
  31.             }
  32.             if ((s[i] >= 'a' && s[i] <= 'z') && (s[j] >= 'a' && s[j] <= 'z') && !visited[i] && !visited[j]) {
  33.                 visited[j] = true;
  34.                 swap(s[i], s[j]);
  35.                 break;
  36.             } else if ((s[i] >= 'A' && s[i] <= 'Z') && (s[j] >= 'A' && s[j] <= 'Z') && !visited[i] && !visited[j]) {
  37.                 visited[j] = true;
  38.                 swap(s[i], s[j]);
  39.                 break;
  40.             }
  41.         }
  42.     }
  43.     cout << s << '\n';
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement