JosepRivaille

X97552: Xifrat amb tanca de carrils

May 21st, 2015
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. typedef vector<char> Row;
  8. typedef vector<Row> Matrix;
  9.  
  10.  
  11. int count(const string& s) {
  12.     int aux = s.size();
  13.     int count = 0;
  14.     for (int i = 0; i < aux; ++i) {
  15.         if (s[i] != '$') ++count;
  16.     }
  17.     return count;  
  18. }
  19.  
  20.  
  21. void codify(Matrix& M,const string& s) {
  22.     int f = M.size();
  23.     int c = M[0].size();
  24.     int iterador = 0;
  25.     int k = 0;
  26.     bool augmentar = true;
  27.     for (int i = 0; i < c; ++i) {
  28.         for (int j = 0; j < f; ++j) {
  29.             if (s[k] == '$') ++k;
  30.             if (iterador == j) M[j][i] = s[k];
  31.             else M[j][i] = '.';
  32.         }
  33.         ++k;
  34.         if (augmentar && iterador < f - 1) ++iterador;
  35.         else if (augmentar && iterador == f - 1) {
  36.             --iterador;
  37.             augmentar = false;
  38.         }
  39.         else if (not augmentar && iterador > 0) --iterador;
  40.         else if (not augmentar && iterador == 0) {
  41.             ++iterador;
  42.             augmentar = true;
  43.         }
  44.     }
  45. }
  46.  
  47. void write_matrix(Matrix& M, bool& first) {
  48.     if (not first) cout << endl;
  49.     else first = false;
  50.     int f = M.size();
  51.     int c = M[0].size();
  52.     for (int i = 0; i < f; ++i) {
  53.         for (int j = 0; j < c; ++j) {
  54.             cout << M[i][j];
  55.         }
  56.         cout << endl;
  57.     }
  58.     cout << endl;
  59. }
  60.  
  61. void write_codified(Matrix& M) {
  62.     int f = M.size();
  63.     int c = M[0].size();
  64.     int count = 0;
  65.     for (int i = 0; i < f; ++i) {
  66.         for (int j = 0; j < c; ++j) {
  67.             if (M[i][j] != '.') {
  68.                 if (count != 0 && count%5 == 0) cout << " ";
  69.                 ++count;
  70.                 cout << M[i][j];
  71.             }
  72.         }
  73.     }
  74.     cout << endl;
  75. }
  76.  
  77. //Pre: Llegeix un enter, una string
  78. //Post: La codifica i l'escriu en una matriu d'n x llargada paraula
  79. int main() {
  80.     int f;
  81.     string s;
  82.     bool first = true;
  83.     while (cin >> f) {
  84.         cin >> s;
  85.         int c = count(s);
  86.         Matrix M(f,Row(c));
  87.         codify(M, s);
  88.         write_matrix(M, first);
  89.         write_codified(M);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment