Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. char solve_matrix[5][5];
  5. string code_word = "WHEATSON";
  6. pair <int, int> pos[200];
  7.  
  8. bool is_bad(char c) {
  9.     return c == ' ' || c == 'J';
  10. }
  11.  
  12.  
  13. ///разбивает строку на двойки
  14. vector <string> parse_string (string s) {
  15.     ///все буквы на большие и удаляет пробелы с J
  16.     transform(s.begin(), s.end(), s.begin(), ::toupper);
  17.     auto ptr = remove_if(s.begin(), s.end(), is_bad);
  18.     s.resize(ptr - s.begin());
  19.    
  20.     ///правила из вики
  21.     vector <string> parsed;
  22.     for (int i = 0; i < s.length(); i += 2) {
  23.         string n = s.substr(i, 2);
  24.         if (n.size() == 1) {
  25.             n += "X";
  26.             parsed.push_back(n);
  27.             continue;
  28.         }
  29.         if (n[0] == n[1]) {
  30.             n[1] = 'X';
  31.             i--;
  32.         }
  33.         parsed.push_back(n);
  34.     }
  35.     return parsed;
  36. }
  37.  
  38. ///построение матрицы
  39. void init_matrix(string code_word) {
  40.     transform(code_word.begin(), code_word.end(), code_word.begin(), ::toupper);
  41.     vector <bool> is_used(26, false);
  42.     int x = 0, y = 0;
  43.    
  44.     ///по всем буквам кодового и установка неповторяющихся
  45.     for (auto c: code_word) {
  46.         if (!is_used[c - 'A']) {
  47.             is_used[c - 'A'] = true;
  48.             solve_matrix[x][y] = c;
  49.             y++;
  50.             if (y == 5)
  51.                 x++, y = 0;
  52.         }
  53.     }
  54.     ///добавление всех остальных в алфавитном порялке
  55.     for (int c = 'A'; c <= 'Z'; c++) {
  56.         if (is_bad(c))
  57.             continue;
  58.         if (!is_used[c - 'A']) {
  59.             is_used[c - 'A'] = true;
  60.             solve_matrix[x][y] = c;
  61.             y++;
  62.             if (y == 5)
  63.                 x++, y = 0;
  64.         }
  65.     }
  66.     ///сохранили позиции символов
  67.     for (int i = 0; i < 5; i++)
  68.         for (int j = 0; j < 5; j++)
  69.             pos[solve_matrix[i][j]] = make_pair(i, j);
  70. }
  71.  
  72. ///сама шифровка
  73. string hide(vector <string> parsed) {
  74.     string res = "";
  75.     for (auto x: parsed) {
  76.         cout << x << ' ';
  77.         ///тут получили позиции символов
  78.         pair <int, int> p1, p2;
  79.         p1 = pos[x[0]];
  80.         p2 = pos[x[1]];
  81.         ///ну и опять правила из вики
  82.         if (p1.first != p2.first && p1.second != p2.second) {
  83.             res += solve_matrix[p1.first][p2.second];
  84.             res += solve_matrix[p2.first][p1.second];
  85.         }
  86.         if (p1.first == p2.first) {
  87.             res += (p1.second == 4? solve_matrix[p1.first][0] : solve_matrix[p1.first][p1.second + 1]);
  88.             res += (p2.second == 4? solve_matrix[p2.first][0] : solve_matrix[p2.first][p2.second + 1]);
  89.         }
  90.         if (p1.second == p2.second) {
  91.             res += (p1.first == 4? solve_matrix[0][p1.second] : solve_matrix[p1.first + 1][p1.second]);
  92.             res += (p2.first == 4? solve_matrix[0][p2.second] : solve_matrix[p2.first + 1][p2.second]);
  93.         }
  94.     }
  95.     cout << "\n";
  96.     return res;
  97. }
  98.  
  99. void solve() {
  100.     ///построили матрицу, посмотрели на нее
  101.     init_matrix(code_word);
  102.     for (int i = 0; i < 5; i++) {
  103.         for (int j = 0; j < 5; j++)
  104.             cout << solve_matrix[i][j];
  105.         cout << "\n";
  106.     }
  107.     ///взяли строку и зашифровали
  108.     string input = "IDIOCY OFTEN LOOKS LIKE INTELLIGENCE";
  109.     cout << hide(parse_string(input));
  110. }
  111.  
  112. int main() {
  113.     solve();
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement