Source code for submission s649

fs.cpp

  1. #include <cstdio>
  2. #include <string>
  3. #include <iostream>
  4. #include <cctype>
  5. #include <algorithm>
  6. #include <map>
  7. using namespace std;
  8. int main()
  9. {
  10. string morse[30];
  11. int i = 0;
  12. morse[i++] = ".-";
  13. morse[i++] = "-...";
  14. morse[i++] = "-.-.";
  15. morse[i++] = "-..";
  16. morse[i++] = ".";
  17. morse[i++] = "..-.";
  18. morse[i++] = "--.";
  19. morse[i++] = "....";
  20. morse[i++] = "..";
  21. morse[i++] = ".---";
  22. morse[i++] = "-.-";
  23. morse[i++] = ".-..";
  24. morse[i++] = "--";
  25. morse[i++] = "-.";
  26. morse[i++] = "---";
  27. morse[i++] = ".--.";
  28. morse[i++] = "--.-";
  29. morse[i++] = ".-.";
  30. morse[i++] = "...";
  31. morse[i++] = "-";
  32. morse[i++] = "..-";
  33. morse[i++] = "...-";
  34. morse[i++] = ".--";
  35. morse[i++] = "-..-";
  36. morse[i++] = "-.--";
  37. morse[i++] = "--..";
  38. morse[26] = "..--";
  39. morse[27] = ".-.-";
  40. morse[28] = "---.";
  41. morse[29] = "----";
  42.  
  43. map<string, char> encode_map;
  44. for(int i = 0; i < 26; ++i)
  45. {
  46. encode_map[morse[i]] = i + 'A';
  47. }
  48. encode_map[morse[26]] = '_';
  49. encode_map[morse[27]] = ',';
  50. encode_map[morse[28]] = '.';
  51. encode_map[morse[29]] = '?';
  52. string line;
  53. getline(cin, line);
  54. while(!cin.eof())
  55. {
  56. int lengths[1000];
  57. string encoded = "";
  58. for(i = 0; i < line.length(); ++i)
  59. {
  60. if(isalnum(line[i]))
  61. {
  62. lengths[i] = morse[line[i] - 'A'].length();
  63. encoded += morse[line[i] - 'A'];
  64. //cout << line[i] << " " << morse[line[i] - 'A'] << " " << lengths[i] << endl;
  65. }
  66. else
  67. {
  68. string code;
  69. switch(line[i])
  70. {
  71. case '_':
  72. code = morse[26];
  73. break;
  74. case ',':
  75. code = morse[27];
  76. break;
  77. case '.':
  78. code += morse[28];
  79. break;
  80. default:
  81. code += morse[29];
  82. break;
  83. }
  84. encoded += code;
  85. lengths[i] = code.length();
  86. //cout << line[i] << " " << code << " " << lengths[i] << endl;
  87. }
  88.  
  89. //reverse(lengths, lengths + line.length());
  90. }
  91. int j = 0;
  92. string res;
  93. /*for(i = 0; i < line.length(); ++i)
  94. {
  95. //cout << lengths[i] << " ,";
  96. }*/
  97. //cout << endl;
  98. for(i = line.length() - 1; i >= 0; --i)
  99. {
  100.  
  101. string morse_char = encoded.substr(j, lengths[i]);
  102. //cout << "i " << i << " j " << j << " length " << lengths[i] << endl << " morse " << morse_char << endl;
  103. j += lengths[i];
  104. res += encode_map[morse_char];
  105. }
  106. cout << res << endl;
  107. getline(cin, line);
  108. }
  109. return 0;
  110. }
  111.