fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <utility>
  5. #include <string>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9. map<char,int> m;
  10. map<int,char> mr;
  11. void preprocess()
  12. {
  13. for(int i=0;i<26;i++){
  14. m['A'+i]=i+1;
  15. mr[i+1]='A'+i;
  16. }
  17. m['\0']=26;
  18. mr[26]='\0';
  19. }
  20.  
  21. /*
  22. void dispMatrix(pair<int**,<pair<int,int> > > x)
  23. {
  24. int m=x.second.first,n=x.second.first;
  25. int** mat = x.first;
  26. for(int i=0;i<m;i++)
  27. {
  28. cout<<"|";
  29. for(int j=0;j<nj++)
  30. {
  31. cout<<mat[i][j]<<" ";
  32. }
  33. cout<<"|"<<endl;
  34. }
  35. }*/
  36. pair<int**,<pair<int,int> > > matrixMultiply(int** x,int m,int n,int** y)
  37. {
  38. //Here m is the columns of x and size of y(for simplicity taken as a square matrix) || n is rows of y
  39. int mul[m][n],c=0;
  40. for(int i=0;i<n;i++)
  41. {
  42. for(int j=0;j<m;j++)
  43. {
  44. mul[i][j] = 0;
  45. for(int k=0;k<m;k++)
  46. {
  47. mul[i][j]+=x[i][k]*y[k][i];
  48. }
  49. mul[i][j] = mul[i][j]%26;
  50. }
  51. }
  52. return make_pair(mul,make_pair(m,n));
  53. }
  54. pair<int**,<pair<int,int> > > string2Matrix(string s)
  55. {
  56. int l=s.length();
  57. int n=(int)ceil(l/5.0);
  58. int mat[5][n],c=0;
  59.  
  60. for(int i=0;i<5;i++)
  61. {
  62. for(int j=0;j<n;j++)
  63. {
  64. if(c<l)
  65. mat[i][j] = m[s[c]];
  66. else
  67. mat[i][j] = '\0';
  68. c++;
  69. }
  70. }
  71. return make_pair(mat,make_pair(5,n));
  72. }
  73. pair<int**,<pair<int,int> > > key2Matrix(string key)
  74. {
  75. int mat[5][5],c=0;
  76.  
  77. for(int i=0;i<5;i++)
  78. {
  79. for(int j=0;j<5;j++)
  80. {
  81. if(c<l)
  82. mat[i][j] = m[s[c]];
  83. else
  84. mat[i][j] = "\0";
  85. c++;
  86. }
  87. }
  88. return make_pair(mat,make_pair(5,n));
  89. }
  90.  
  91. string matrix2String(pair<int**,<pair<int,int> > > x)
  92. {
  93. int m=x.second.first,n=x.second.second;
  94. string out="";
  95. for(int i=0;i<m;i++)
  96. {
  97. for(int j=0;j<n;j++)
  98. {
  99. out = out + mr[m[i][j]];
  100. }
  101. }
  102. return out;
  103. }
  104.  
  105. string hillCipher(string s,string key)
  106. {
  107. pair<int**,<pair<int,int> > > inp,kk,out;
  108. inp = string2Matrix(s);
  109. kk = key2Matrix(s);
  110. out = matrixMultiply(inp.first,inp.second.first,inp.second.second,kk.first);
  111. return matrix2String(out);
  112. }
  113. int main()
  114. {
  115. preprocess();
  116. string key = "AQUICKBROWNFOXJUMPEDOVERTHELAZYDOG";
  117. string str = "DOGABROWNTHEQUICKJUMPEDFOXLAZYOVER";
  118. cout<<hillCipher(str,key);
  119. }
  120.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:36:29: error: template argument 2 is invalid
 pair<int**,<pair<int,int> > > matrixMultiply(int** x,int m,int n,int** y)
                             ^
prog.cpp: In function 'int matrixMultiply(int**, int, int, int**)':
prog.cpp:52:37: error: no matching function for call to 'make_pair(int [m][n], std::pair<int, int>)'
  return make_pair(mul,make_pair(m,n));
                                     ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:286:5: note: candidate: template<class _T1, class _T2> std::pair<_T1, _T2> std::make_pair(_T1, _T2)
     make_pair(_T1 __x, _T2 __y)
     ^
/usr/include/c++/5/bits/stl_pair.h:286:5: note:   template argument deduction/substitution failed:
prog.cpp:52:37: note:   variable-sized array type 'int (*)[n]' is not a valid template argument
  return make_pair(mul,make_pair(m,n));
                                     ^
prog.cpp: At global scope:
prog.cpp:54:29: error: template argument 2 is invalid
 pair<int**,<pair<int,int> > > string2Matrix(string s)
                             ^
prog.cpp: In function 'int string2Matrix(std::string)':
prog.cpp:71:37: error: no matching function for call to 'make_pair(int [5][n], std::pair<int, int>)'
  return make_pair(mat,make_pair(5,n));
                                     ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:286:5: note: candidate: template<class _T1, class _T2> std::pair<_T1, _T2> std::make_pair(_T1, _T2)
     make_pair(_T1 __x, _T2 __y)
     ^
/usr/include/c++/5/bits/stl_pair.h:286:5: note:   template argument deduction/substitution failed:
prog.cpp:71:37: note:   variable-sized array type 'int (*)[n]' is not a valid template argument
  return make_pair(mat,make_pair(5,n));
                                     ^
prog.cpp: At global scope:
prog.cpp:73:29: error: template argument 2 is invalid
 pair<int**,<pair<int,int> > > key2Matrix(string key)
                             ^
prog.cpp: In function 'int key2Matrix(std::string)':
prog.cpp:81:9: error: 'l' was not declared in this scope
    if(c<l)
         ^
prog.cpp:82:18: error: 's' was not declared in this scope
    mat[i][j] = m[s[c]];
                  ^
prog.cpp:84:14: error: invalid conversion from 'const char*' to 'int' [-fpermissive]
    mat[i][j] = "\0";
              ^
prog.cpp:88:35: error: 'n' was not declared in this scope
  return make_pair(mat,make_pair(5,n));
                                   ^
prog.cpp: At global scope:
prog.cpp:91:50: error: template argument 2 is invalid
 string matrix2String(pair<int**,<pair<int,int> > > x)
                                                  ^
prog.cpp:91:50: error: template argument 2 is invalid
prog.cpp:91:50: error: template argument 2 is invalid
prog.cpp:92:1: error: expected ',' or ';' before '{' token
 {
 ^
stdout
Standard output is empty