Source code for submission s510

fs.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #include <string>
  7. #include <map>
  8.  
  9. int i, j, k;
  10. int i2, j2, k2;
  11.  
  12. std::map<char, std::string> toMorse;
  13. std::map<std::string, char> fromMorse;
  14.  
  15. char line[1024];
  16. std::string morse;
  17. int morseLen[1024];
  18.  
  19. int fillTables()
  20. {
  21. toMorse['A'] = ".-";
  22. toMorse['B'] = "-...";
  23. toMorse['C'] = "-.-.";
  24. toMorse['D'] = "-..";
  25. toMorse['E'] = ".";
  26. toMorse['F'] = "..-.";
  27. toMorse['G'] = "--.";
  28. toMorse['H'] = "....";
  29. toMorse['I'] = "..";
  30. toMorse['J'] = ".---";
  31. toMorse['K'] = "-.-";
  32. toMorse['L'] = ".-..";
  33. toMorse['M'] = "--";
  34. toMorse['N'] = "-.";
  35. toMorse['O'] = "---";
  36. toMorse['P'] = ".--.";
  37. toMorse['Q'] = "--.-";
  38. toMorse['R'] = ".-.";
  39. toMorse['S'] = "...";
  40. toMorse['T'] = "-";
  41. toMorse['U'] = "..-";
  42. toMorse['V'] = "...-";
  43. toMorse['W'] = ".--";
  44. toMorse['X'] = "-..-";
  45. toMorse['Y'] = "-.--";
  46. toMorse['Z'] = "--..";
  47. toMorse['_'] = "..--";
  48. toMorse[','] = ".-.-";
  49. toMorse['.'] = "---.";
  50. toMorse['?'] = "----";
  51.  
  52. for (std::map<char, std::string>::iterator it = toMorse.begin(); it != toMorse.end(); it++)
  53. {
  54. fromMorse[it->second] = it->first;
  55. }
  56.  
  57. return 0;
  58. }
  59.  
  60. /* ------------------------------- */
  61. int main()
  62. {
  63. fillTables();
  64. int len;
  65. while (scanf("%s", line) == 1)
  66. {
  67. morse = "";
  68. len = strlen(line);
  69. for (i = 0; i < len; i++)
  70. {
  71. std::string& s = toMorse[line[i]];
  72. morse += s;
  73. morseLen[i] = s.length();
  74. }
  75.  
  76. /*
  77.   printf("%s", morse.c_str());
  78.   for (i = 0; i < len; i++)
  79.   {
  80.   printf("%d", morseLen[i]);
  81.   }
  82.   printf("\n");
  83.   */
  84.  
  85. int pos = 0;
  86. for (i = len-1; i >= 0; i--)
  87. {
  88. std::string found;
  89. for (j = 0; j < morseLen[i]; j++)
  90. found += morse[pos + j];
  91.  
  92. putchar(fromMorse[found]);
  93. pos += morseLen[i];
  94. }
  95.  
  96. printf("\n");
  97. }
  98.  
  99. return 0;
  100. }
  101.