Source code for submission s675

fs.cpp

  1. #include <iostream>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <complex>
  5. #include <cstdio>
  6. #include <string>
  7. #include <list>
  8. #include <map>
  9. #include <queue>
  10. #include <set>
  11. #include <sstream>
  12. #include <stack>
  13. #include <utility>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. #define DEBUG(x) cout << ">>> " #x << " : " << x << endl;
  19.  
  20. void transform(char chr, std::vector<bool> & crypt, std::vector<unsigned> & len)
  21. {
  22.  
  23. bool T(true);
  24. bool F(false);
  25.  
  26. switch(chr)
  27. {
  28. case 'A':
  29. len.push_back(2);
  30. crypt.push_back(F);
  31. crypt.push_back(T);
  32. break;
  33. case 'B':
  34. len.push_back(4);
  35. crypt.push_back(T);
  36. crypt.push_back(F);
  37. crypt.push_back(F);
  38. crypt.push_back(F);
  39. break;
  40. case 'C':
  41. len.push_back(4);
  42. crypt.push_back(T);
  43. crypt.push_back(F);
  44. crypt.push_back(T);
  45. crypt.push_back(F);
  46. break;
  47. case 'D':
  48. len.push_back(3);
  49. crypt.push_back(T);
  50. crypt.push_back(F);
  51. crypt.push_back(F);
  52. break;
  53. case 'E':
  54. len.push_back(1);
  55. crypt.push_back(F);
  56. break;
  57. case 'F':
  58. len.push_back(4);
  59. crypt.push_back(F);
  60. crypt.push_back(F);
  61. crypt.push_back(T);
  62. crypt.push_back(F);
  63. break;
  64. case 'G':
  65. len.push_back(3);
  66. crypt.push_back(T);
  67. crypt.push_back(T);
  68. crypt.push_back(F);
  69. break;
  70. case 'H':
  71. len.push_back(4);
  72. crypt.push_back(F);
  73. crypt.push_back(F);
  74. crypt.push_back(F);
  75. crypt.push_back(F);
  76. break;
  77. case 'I':
  78. len.push_back(2);
  79. crypt.push_back(F);
  80. crypt.push_back(F);
  81. break;
  82. case 'J':
  83. len.push_back(4);
  84. crypt.push_back(F);
  85. crypt.push_back(T);
  86. crypt.push_back(T);
  87. crypt.push_back(T);
  88. break;
  89. case 'K':
  90. len.push_back(3);
  91. crypt.push_back(T);
  92. crypt.push_back(F);
  93. crypt.push_back(T);
  94. break;
  95. case 'L':
  96. len.push_back(4);
  97. crypt.push_back(F);
  98. crypt.push_back(T);
  99. crypt.push_back(F);
  100. crypt.push_back(F);
  101. break;
  102. case 'M':
  103. len.push_back(2);
  104. crypt.push_back(T);
  105. crypt.push_back(T);
  106. break;
  107. case 'N':
  108. len.push_back(2);
  109. crypt.push_back(T);
  110. crypt.push_back(F);
  111. break;
  112. case 'O':
  113. len.push_back(3);
  114. crypt.push_back(T);
  115. crypt.push_back(T);
  116. crypt.push_back(T);
  117. break;
  118. case 'P':
  119. len.push_back(4);
  120. crypt.push_back(F);
  121. crypt.push_back(T);
  122. crypt.push_back(T);
  123. crypt.push_back(F);
  124. break;
  125. case 'Q':
  126. len.push_back(4);
  127. crypt.push_back(T);
  128. crypt.push_back(T);
  129. crypt.push_back(F);
  130. crypt.push_back(T);
  131. break;
  132. case 'R':
  133. len.push_back(3);
  134. crypt.push_back(F);
  135. crypt.push_back(T);
  136. crypt.push_back(F);
  137. break;
  138. case 'S':
  139. len.push_back(3);
  140. crypt.push_back(F);
  141. crypt.push_back(F);
  142. crypt.push_back(F);
  143. break;
  144. case 'T':
  145. len.push_back(1);
  146. crypt.push_back(T);
  147. break;
  148. case 'U':
  149. len.push_back(3);
  150. crypt.push_back(F);
  151. crypt.push_back(F);
  152. crypt.push_back(T);
  153. break;
  154. case 'V':
  155. len.push_back(4);
  156. crypt.push_back(F);
  157. crypt.push_back(F);
  158. crypt.push_back(F);
  159. crypt.push_back(T);
  160. break;
  161. case 'W':
  162. len.push_back(3);
  163. crypt.push_back(F);
  164. crypt.push_back(T);
  165. crypt.push_back(T);
  166. break;
  167. case 'X':
  168. len.push_back(4);
  169. crypt.push_back(T);
  170. crypt.push_back(F);
  171. crypt.push_back(F);
  172. crypt.push_back(T);
  173. break;
  174. case 'Y':
  175. len.push_back(4);
  176. crypt.push_back(T);
  177. crypt.push_back(F);
  178. crypt.push_back(T);
  179. crypt.push_back(T);
  180. break;
  181. case 'Z':
  182. len.push_back(4);
  183. crypt.push_back(T);
  184. crypt.push_back(T);
  185. crypt.push_back(F);
  186. crypt.push_back(F);
  187. break;
  188. case '_':
  189. len.push_back(4);
  190. crypt.push_back(F);
  191. crypt.push_back(F);
  192. crypt.push_back(T);
  193. crypt.push_back(T);
  194. break;
  195. case '.':
  196. len.push_back(4);
  197. crypt.push_back(T);
  198. crypt.push_back(T);
  199. crypt.push_back(T);
  200. crypt.push_back(F);
  201. break;
  202. case ',':
  203. len.push_back(4);
  204. crypt.push_back(F);
  205. crypt.push_back(T);
  206. crypt.push_back(F);
  207. crypt.push_back(T);
  208. break;
  209. case '?':
  210. len.push_back(4);
  211. crypt.push_back(T);
  212. crypt.push_back(T);
  213. crypt.push_back(T);
  214. crypt.push_back(T);
  215. break;
  216. }
  217. }
  218.  
  219. char decrypt(unsigned len, std::vector<bool>::iterator & cryptPos)
  220. {
  221.  
  222. bool last(*cryptPos);
  223. ++cryptPos;
  224.  
  225. if (len > 1)
  226. {
  227. if (last)
  228. {
  229. last = *cryptPos;
  230. ++cryptPos;
  231.  
  232. if (len > 2)
  233. {
  234.  
  235. if (last)
  236. {
  237. last = *cryptPos;
  238. ++cryptPos;
  239.  
  240. if (len > 3)
  241. {
  242. if (last)
  243. {
  244. last = *cryptPos;
  245. ++cryptPos;
  246.  
  247. if(last) // TTTT
  248. return '?';
  249. else // TTTF
  250. return '.';
  251. }
  252. else
  253. {
  254. last = *cryptPos;
  255. ++cryptPos;
  256.  
  257. if(last) // TTFT
  258. return 'Q';
  259. else // TTFF
  260. return 'Z';
  261. }
  262. }
  263. else
  264. {
  265. if(last) // TTT
  266. return 'O';
  267. else // TTF
  268. return 'G';
  269. }
  270. }
  271. else
  272. {
  273. last = *cryptPos;
  274. ++cryptPos;
  275.  
  276. if (len > 3)
  277. {
  278. if (last)
  279. {
  280. last = *cryptPos;
  281. ++cryptPos;
  282.  
  283. if(last) // TFTT
  284. return 'Y';
  285. else // TFTF
  286. return 'C';
  287. }
  288. else
  289. {
  290. last = *cryptPos;
  291. ++cryptPos;
  292.  
  293. if(last) // TFFT
  294. return 'X';
  295. else // TFFF
  296. return 'B';
  297. }
  298. }
  299. else
  300. {
  301. if(last) // TFT
  302. return 'K';
  303. else // TFF
  304. return 'D';
  305. }
  306. }
  307.  
  308. }
  309. else
  310. {
  311. if(last) // TT
  312. return 'M';
  313. else // TF
  314. return 'N';
  315. }
  316. }
  317. else
  318. {
  319. last = *cryptPos;
  320. ++cryptPos;
  321.  
  322. if (len > 2)
  323. {
  324. if (last)
  325. {
  326. last = *cryptPos;
  327. ++cryptPos;
  328.  
  329. if (len > 3)
  330. {
  331. if (last)
  332. {
  333. last = *cryptPos;
  334. ++cryptPos;
  335.  
  336. if(last) // FTTT
  337. return 'J';
  338. else // FTTF
  339. return 'P';
  340. }
  341. else
  342. {
  343. last = *cryptPos;
  344. ++cryptPos;
  345.  
  346. if(last) // FTFT
  347. return ',';
  348. else // FTFF
  349. return 'L';
  350. }
  351. }
  352. else
  353. {
  354. if(last) // FTT
  355. return 'W';
  356. else // FTF
  357. return 'R';
  358. }
  359. }
  360. else
  361. {
  362. last = *cryptPos;
  363. ++cryptPos;
  364.  
  365. if (len > 3)
  366. {
  367. if (last)
  368. {
  369. last = *cryptPos;
  370. ++cryptPos;
  371.  
  372. if(last) // FFTT
  373. return '_';
  374. else // FFTF
  375. return 'F';
  376. }
  377. else
  378. {
  379. last = *cryptPos;
  380. ++cryptPos;
  381.  
  382. if(last) // FFFT
  383. return 'V';
  384. else // FFFF
  385. return 'H';
  386. }
  387.  
  388. }
  389. else
  390. {
  391. if(last) // FFT
  392. return 'U';
  393. else // FFF
  394. return 'S';
  395. }
  396. }
  397. }
  398. else
  399. {
  400. if(last) // FT
  401. return 'A';
  402. else // FF
  403. return 'I';
  404. }
  405. }
  406. }
  407. else
  408. {
  409. if(last)
  410. return 'T';
  411. else
  412. return 'E';
  413. }
  414. }
  415.  
  416. std::string convert(std::string & str)
  417. {
  418. std::vector<bool> crypt;
  419. std::vector<unsigned> len;
  420. for (std::string::iterator pnter(str.begin()); pnter!=str.end(); ++pnter)
  421. transform(*pnter, crypt, len);
  422.  
  423.  
  424. std::vector<char> final;
  425. std::vector<bool>::iterator cryptPos(crypt.begin());
  426. for (std::vector<unsigned>::reverse_iterator pnter(len.rbegin()); pnter!=len.rend(); ++pnter)
  427. {
  428.  
  429. final.push_back(decrypt(*pnter, cryptPos));
  430.  
  431. }
  432.  
  433. return std::string(final.begin(), final.end());
  434. }
  435.  
  436. int main()
  437. {
  438. std::string line;
  439. while (std::cin>>line)
  440. {
  441. std::cout << convert(line) << std::endl;
  442. }
  443. }
  444.