Advertisement
shek_shek

Algebra_7

Mar 6th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stack>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <string>
  10. #include <set>
  11. #include <iomanip>
  12. #include <vector>
  13. #include <map>
  14. #include <cassert>
  15. #include <queue>
  16. #include <fstream>
  17.  
  18. using namespace std;
  19.  
  20. typedef long long li;
  21. typedef long double ld;
  22.  
  23. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  24. #define pb push_back
  25. #define mp make_pair
  26. #define all(v) v.begin(),v.end()
  27. #define EPS 1e-9
  28. #define PI 3.1415926535897932384626433832795
  29.  
  30.  
  31. int gcd (int a, int b) {
  32.     return b ? gcd (b, a % b) : a;
  33. }
  34.  
  35. int main() {
  36.     srand(time(NULL));
  37.     ifstream key_stream("key.txt");
  38.     ifstream text_stream("text.txt");
  39.     freopen("output.txt", "w", stdout);
  40.  
  41.     string alph = "абвгдежзийклмнопрстуфхцчшщъыьэюя";
  42.     map <char, bool> check;
  43.     forn (i, alph.size())
  44.         check[alph[i]] = false;
  45.  
  46.     string key;
  47.     key_stream >> key;
  48.  
  49.  
  50.     if (key.size() < 3) {
  51.         cout << "INCORRECT KEY" << endl;
  52.         return 0;
  53.     }
  54.  
  55.     if (key.size() > 7)
  56.         key = key.substr(0, 7);
  57.  
  58.  
  59.     string temp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
  60.     map <char, string> alph_to_pair;
  61.     int alph_cnt = 0;
  62.  
  63.     forn (i, key.size()) {
  64.         if (!check[key[i]]) {
  65.             alph_to_pair[key[i]] = temp[i];
  66.             check[key[i]] = true;
  67.             alph_cnt++;
  68.         }
  69.     }
  70.     int alph_idx = 0;
  71.     for (int i = 7; i < 10; i++) {
  72.         forn (j, 10) {
  73.             if (alph_cnt >= alph.size() || alph_idx >= alph.size())
  74.                 continue;
  75.             while (check[alph[alph_idx]])
  76.                 alph_idx++;
  77.             if (!check[alph[alph_idx]]) {
  78.                 check[alph[alph_idx]] = true;
  79.                 alph_to_pair[alph[alph_idx]] = temp[i] + temp[j];
  80.                 alph_idx++, alph_cnt++;
  81.             }
  82.         }
  83.     }
  84.     string result = "";
  85.     while (!text_stream.eof()) {
  86.         string text;
  87.         text_stream >> text;
  88.         forn (i, text.size()) {
  89.             if (text[i] >= 'А' && text[i] <= 'Я')
  90.                 text[i] += 'а' - 'А';
  91.             result += alph_to_pair[text[i]];
  92.         }
  93.     }
  94.  
  95.     forn (i, result.size()) {
  96.         cout << alph[result[i] - '0'];
  97.     }
  98.  
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  106.  
  107.  
  108.  
  109. #define _CRT_SECURE_NO_WARNINGS
  110.  
  111. #include <stdio.h>
  112. #include <stack>
  113. #include <math.h>
  114. #include <time.h>
  115. #include <iostream>
  116. #include <algorithm>
  117. #include <string>
  118. #include <set>
  119. #include <iomanip>
  120. #include <vector>
  121. #include <map>
  122. #include <cassert>
  123. #include <queue>
  124. #include <fstream>
  125.  
  126. using namespace std;
  127.  
  128. typedef long long li;
  129. typedef long double ld;
  130.  
  131. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  132. #define pb push_back
  133. #define mp make_pair
  134. #define all(v) v.begin(),v.end()
  135. #define EPS 1e-9
  136. #define PI 3.1415926535897932384626433832795
  137.  
  138.  
  139. int gcd (int a, int b) {
  140.     return b ? gcd (b, a % b) : a;
  141. }
  142.  
  143. string to_string (int t) {
  144.     if (t == 0)
  145.         return "0";
  146.     string result = "";
  147.     while (t > 0) {
  148.         result += '0' + t % 10;
  149.         t /= 10;
  150.     }
  151.     return result;
  152. }
  153.  
  154. int main() {
  155.     srand(time(NULL));
  156.     ifstream key_stream("key.txt");
  157.     ifstream text_stream("text.txt");
  158.     freopen("output.txt", "w", stdout);
  159.  
  160.     string alph = "абвгдежзийклмнопрстуфхцчшщъыьэюя";
  161.     map <char, bool> check;
  162.     forn (i, alph.size())
  163.         check[alph[i]] = false;
  164.  
  165.     string key;
  166.     key_stream >> key;
  167.    
  168.     if (key.size() < 3) {
  169.         cout << "INCORRECT KEY" << endl;
  170.         return 0;
  171.     }
  172.  
  173.     if (key.size() > 7)
  174.         key = key.substr(0, 7);
  175.  
  176.     string temp[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
  177.     map <string, char> pair_to_alph;
  178.     int alph_idx = 0, alph_cnt = 0;
  179.  
  180.     forn (i, key.size()) {
  181.         if (!check[key[i]]) {
  182.             pair_to_alph[temp[i]] = key[i];
  183.             alph_cnt++;
  184.             check[key[i]] = true;
  185.         }
  186.     }
  187.     for (int i = 7; i < 10; i++) {
  188.         forn (j, 10) {
  189.             if (alph_cnt >= alph.size() || alph_idx >= alph.size())
  190.                 continue;
  191.             while (check[alph[alph_idx]])
  192.                 alph_idx++;
  193.             if (!check[alph[alph_idx]]) {
  194.                 check[alph[alph_idx]] = true;
  195.                 pair_to_alph[temp[i] + temp[j]] = alph[alph_idx];
  196.                 alph_idx++, alph_cnt++;
  197.             }
  198.         }
  199.     }
  200.  
  201.     string text;
  202.     text_stream >> text;
  203.  
  204.     for (size_t i = 0; i < text.size(); i += 2) {
  205.         if (text[i] != alph[8] && text[i] != alph[9] && text[i] != alph[0]) {          
  206.             int idx = alph.find(text[i]);
  207.             string ss = to_string(idx);
  208.             cout << pair_to_alph[ss];
  209.             i--;
  210.         } else {
  211.             int idx1 = alph.find(text[i]), idx2 = alph.find(text[i + 1]);
  212.             string ss = to_string(idx1) + to_string(idx2);
  213.             cout << pair_to_alph[ss];
  214.         }
  215.     }
  216.  
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement