Source code for submission s544

fs.cpp

  1. //
  2. // File: code.cc
  3. // Author: cteam019
  4. //
  5. // Created on October 19, 2013, 10:42 AM
  6. //
  7.  
  8. #include <stdlib.h>
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <vector>
  13. #include <map>
  14. #include <string>
  15.  
  16. char g_Buffer[1024];
  17. //const char* g_Buffer = "FENDSVTSLHW.EDATS,EULAY";
  18.  
  19. typedef std::map<char, std::string> c2smap;
  20. typedef c2smap::iterator c2siter;
  21. c2smap g_Table;
  22.  
  23. typedef std::map<std::string, char> s2cmap;
  24. typedef s2cmap::iterator s2citer;
  25.  
  26. s2cmap g_ToLetter;
  27. std::string g_Decoded;
  28.  
  29. std::vector<int> g_Lenghts;
  30. std::string g_Morse;
  31. typedef std::string::iterator StrIter;
  32. void do_stuff(){
  33. g_Buffer[0] = 0;
  34. gets(g_Buffer);
  35. if ( strlen(g_Buffer) == 0 ) exit(EXIT_SUCCESS);
  36. g_Lenghts.clear();
  37. g_Morse.clear();
  38. g_Decoded.clear();
  39.  
  40. int i = 0;
  41. while(g_Buffer[i] != 0) {
  42. std::string& rep = g_Table[g_Buffer[i]];
  43. g_Morse.append(rep);
  44. g_Lenghts.push_back(rep.size());
  45. ++i;
  46.  
  47. }
  48.  
  49. //std::reverse(g_Lenghts.begin(), g_Lenghts.end());
  50.  
  51. int pos = 0;
  52. while (!g_Lenghts.empty()) {
  53. int len = g_Lenghts.back();
  54. g_Lenghts.pop_back();
  55. std::string bit = g_Morse.substr(pos, len);
  56. g_Decoded += g_ToLetter[bit];
  57. pos += len;
  58. }
  59.  
  60. puts(g_Decoded.c_str());
  61. }
  62.  
  63. void init(){
  64. g_Morse.reserve(10000);
  65. g_Table['A'] = ".-";//A
  66. g_Table['B'] = "-...";//A
  67. g_Table['C'] = "-.-.";//A
  68. g_Table['D'] = "-..";//A
  69. g_Table['E'] = ".";//A
  70. g_Table['F'] = "..-.";//A
  71. g_Table['G'] = "--.";//A
  72. g_Table['H'] = "....";//A
  73. g_Table['I'] = "..";//A
  74. g_Table['J'] = ".---";//A
  75. g_Table['K'] = "-.-";//A
  76. g_Table['L'] = ".-..";//A
  77. g_Table['M'] = "--";//A
  78. g_Table['N'] = "-.";//A
  79. g_Table['O'] = "---";//A
  80. g_Table['P'] = ".--.";//A
  81. g_Table['Q'] = "--.-";//A
  82. g_Table['R'] = ".-.";//A
  83. g_Table['S'] = "...";//A
  84. g_Table['T'] = "-";//A
  85. g_Table['U'] = "..-";//A
  86. g_Table['V'] = "...-";//A
  87. g_Table['W'] = ".--";//A
  88. g_Table['X'] = "-..-";//A
  89. g_Table['Y'] = "-.--";//A
  90. g_Table['Z'] = "--..";//A
  91. g_Table['_'] = "..--";//A
  92. g_Table[','] = ".-.-";//A
  93. g_Table['.'] = "---.";//A
  94. g_Table['?'] = "----";//A
  95.  
  96. for ( c2siter it = g_Table.begin(), end = g_Table.end(); it != end; ++it )
  97. g_ToLetter[it->second] = it->first;
  98.  
  99. }
  100.  
  101. //
  102. //
  103. //
  104. int main(int argc, char** argv) {
  105.  
  106. init();
  107.  
  108. while(!feof(stdin)){
  109. do_stuff();
  110. }
  111.  
  112. return (EXIT_SUCCESS);
  113. }
  114.  
  115.