Advertisement
yordanganev

pc2017C

May 5th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4.  
  5. #include <vector>
  6.  
  7. #define print(var) ;//cout << #var << ": " << var<< endl;
  8.  
  9. using namespace std;
  10.  
  11. string decode(string inp, int base);
  12. string code(string inp, int base);
  13.  
  14. void reverseString(string* str) {
  15.     int length = str->length();
  16.     for (int i = 0; i < (length / 2); i++) {
  17.         char tmp = str->at(i);
  18.         str->at(i) = str->at(length - 1 - i);
  19.         str->at(length - 1 - i) = tmp;
  20.     }
  21. }
  22.  
  23.  
  24. int main() {
  25.     int tests;
  26.     cin >> tests;
  27.     cin.ignore();
  28.  
  29.  
  30.     while (tests--) {
  31.         int base;
  32.         int op;
  33.         string inp;
  34.         getline(cin, inp);
  35.         base = (inp.at(0) - '0');
  36.         op = (inp.at(2) - '0');
  37.         inp.erase(0, 4);
  38.         print(base);
  39.         print(op);
  40.         print(inp);
  41.  
  42.         if (op) {
  43.             cout << decode(inp, base) << ":\n";
  44.         }
  45.         else {
  46.             cout << code(inp, base) << ":\n";
  47.         }
  48.     }
  49. }
  50.  
  51. string decode(string inp, int base) {
  52.     int toBase = inp.length() % base;
  53.  
  54.     if (toBase) {
  55.         for (int i = 0; i < base - toBase; i++) {
  56.             inp += " ";
  57.         }
  58.     }
  59.     int loops = inp.length() / base;
  60.  
  61.     string dcode;
  62.     string result;
  63.  
  64.     for (int i = 0; i < loops; i++) {
  65.         dcode = inp.substr(0, base);
  66.         inp.erase(0, base);
  67.         reverseString(&dcode);
  68.         result += dcode;
  69.     }
  70.  
  71.     while (result.find_last_of(' ') == result.length() - 1) {
  72.         result.erase(result.find_last_of(' '));
  73.     }
  74.     return result;
  75. }
  76.  
  77.  
  78. string code(string inp, int base) {
  79.     int toBase = inp.length() % base;
  80.    
  81.     if (toBase) {
  82.         for (int i = 0; i < base - toBase; i++) {
  83.             inp += " ";
  84.         }
  85.     }
  86.  
  87.     int loops = inp.length() / base;
  88.  
  89.     string code;
  90.     string result;
  91.  
  92.     for (int i = 0; i < loops; i++) {
  93.         code = inp.substr(0, base);
  94.         inp.erase(0, base);
  95.         reverseString(&code);
  96.         result += code;
  97.     }
  98.  
  99.     return result;
  100. }
  101.  
  102. /*
  103. 5
  104. 5 0 abcdefghijklmnopqrstuvwxyz
  105. 5 1 edcbajihgfonmlktsrqpyxwvu    z
  106. 2 0 jereme
  107. 2 1 ejerem
  108. 5 1 a
  109.  
  110. 2
  111. 5 1 edcbajihgfonmlktsrqpyxwvu    z
  112. 2 1 ejerem
  113. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement