fork(4) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. bool compare(char a, char b)
  8. {
  9. return(a == b);
  10. }
  11.  
  12. void DiGraph(vector <string> & digraph, string inpCip)
  13. {
  14. string temp;
  15. for(int k = 0; k < inpCip.length(); k+=2)
  16. {
  17. temp = "";
  18. if(inpCip.length() - k >= 2)
  19. {
  20. temp += inpCip[k];
  21. temp += inpCip[k+1];
  22. }
  23. else
  24. {
  25. temp += inpCip[k];
  26. }
  27. digraph.push_back(temp);
  28. }
  29. }
  30.  
  31. void search(int row[1], int col[1], string digraph, string box[5])
  32. {
  33. for(int k = 0; k < 5; k++)
  34. for(int j = 0; j < 5; j++)
  35. {
  36. if(digraph[0] == box[k][j])
  37. {
  38. row[0] = k;
  39. col[0] = j;
  40. }
  41. }
  42.  
  43. for(int k = 0; k < 5; k++)
  44. for(int j = 0; j < 5; j++)
  45. {
  46. if(digraph[1] == box[k][j])
  47. {
  48. row[1] = k;
  49. col[1] = j;
  50. }
  51. }
  52. }
  53.  
  54. int main()
  55. {
  56. string inpDec,inpCip;
  57. vector <char> puz;
  58. int testC;
  59. string alpha = "abcdefghijklmnoprstuvwxyz";
  60. vector <char> ::iterator mii;
  61. vector <char> ::iterator end;
  62. int row[1], col[1];
  63. vector <string> digraph;
  64. cin >> testC;
  65. getline(cin, inpDec);
  66. for(int i = 0; i < testC; i++)
  67. {
  68. string box[5] = {"", "", "", "", ""};
  69.  
  70. getline(cin, inpDec);
  71. getline(cin, inpCip);
  72.  
  73. for(int k = 0; k < inpDec.length() - 1; k++)
  74. for(int j = k + 1; j < inpDec.length(); j++)
  75. {
  76. if(inpDec[k] == inpDec[j])
  77. inpDec[j] = ' ';
  78. }
  79.  
  80. for(int k = 0; k < inpDec.length(); k++)
  81. if(inpDec[k] != ' ')
  82. puz.push_back(inpDec[k]);
  83.  
  84. mii = puz.begin();
  85.  
  86. string tempAlph = alpha;
  87.  
  88. for(;mii != puz.end(); mii++)
  89. {
  90. for(int k = 0; k < tempAlph.length(); k++)
  91. if(*mii == tempAlph[k])
  92. tempAlph[k] = '0';
  93. }
  94.  
  95. mii = puz.begin();
  96. int ind = 0;
  97. for(int k = 0; k < 5; k++)
  98. for(int j = 0; j < 5; j++)
  99. {
  100. if(mii != puz.end())
  101. {
  102. box[k] += *mii;
  103. mii++;
  104. }
  105. else
  106. {
  107. here:;
  108. if(tempAlph[ind] != '0')
  109. box[k] += tempAlph[ind];
  110. else
  111. {
  112. ind++;
  113. goto here;
  114. }
  115. ind++;
  116. }
  117. }
  118.  
  119. for(int k = 0; k < inpCip.length(); k++)
  120. {
  121. if(inpCip[k] == ' ')
  122. {
  123. inpCip.erase(k, 1);
  124. k--;
  125. }
  126. }
  127.  
  128. string temp;
  129.  
  130. bool changes = true;
  131. DiGraph(digraph,inpCip);
  132. while(changes == true)
  133. {
  134. changes = false;
  135. temp = "";
  136.  
  137. // for(int k = 0; k < digraph.size(); k++)
  138. // cout << digraph[k] << " ";
  139. // cout << endl;
  140.  
  141. for(int k = 0; k < digraph.size(); k++)
  142. if(((digraph[k][0] == digraph[k][1]) || digraph[k].length() == 1)
  143. && k < digraph.size())
  144. {
  145. temp += digraph[k][0];
  146. temp += 'x';
  147. if(digraph[k].length() == 2)
  148. temp += digraph[k][1];
  149.  
  150. changes = true;
  151. }
  152. else
  153. temp += digraph[k];
  154.  
  155. // cout << temp << endl;
  156.  
  157. if(changes == true)
  158. {
  159. if(temp.length() % 2 == 1 && temp[temp.length()-1] == 'x')
  160. temp.erase(temp.length()-1,1);
  161. digraph.clear();
  162. DiGraph(digraph,temp);
  163. }
  164.  
  165.  
  166. //for(int k = 0; k < digraph.size(); k++)
  167. // cout << digraph[k] << " ";
  168. //cout << endl;
  169. }
  170.  
  171. for(int k = 0; k < digraph.size(); k++)
  172. {
  173. search(row,col, digraph[k], box);
  174.  
  175. if(digraph[k].size() == 1)
  176. continue;
  177.  
  178. if(row[0] == row[1])
  179. {
  180.  
  181. if(col[0] == 4)
  182. cout << char(toupper(box [row[0]] [0]));
  183. else
  184. cout << char(toupper(box [row[0]] [++col[0]]));
  185.  
  186. if(col[1] == 4)
  187. cout << char(toupper(box [row[1]] [0]));
  188. else
  189. cout << char(toupper(box [row[1]] [++col[1]]));
  190. }
  191. else if(col[0] == col[1])
  192. {
  193. if(row[0] == 4)
  194. cout << char(toupper(box [0][col[0]]));
  195. else
  196. cout << char(toupper(box [++row[0]][col[0]]));
  197.  
  198. if(row[1] == 4)
  199. cout << char(toupper(box [0][col[1]]));
  200. else
  201. cout << char(toupper(box [++row[1]][col[1]]));
  202. }
  203. else
  204. {
  205. cout << char(toupper(box[row[0]][col[1]]))
  206. << char(toupper(box[row[1]][col[0]]));
  207. }
  208.  
  209. }
  210.  
  211. cout << endl;
  212. puz.clear();
  213. digraph.clear();
  214. }
  215.  
  216. return 0;
  217. }
Runtime error #stdin #stdout 0s 3040KB
stdin
2
playfair example
hide the gold in the tree stump
the magic key
i love programming competition
stdout
BRDRZDXDKPBRJZDRLCEHMOTVBI