Source code for submission s489

one.cpp

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include <list>
  8. #include <map>
  9. #include <queue>
  10. #include <set>
  11. #include <sstream>
  12. #include <stack>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. using namespace std;
  17.  
  18. #define DEBUG(x) cerr << ">> " << #x << ": " << x << endl;
  19. #define REP(i,a) for (int i =0; i < (a);++i)
  20. #define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
  21.  
  22. string toMorse[255];
  23. char fromMorse[255];
  24.  
  25. int tobinary(string s)
  26. {
  27. int a = s.length();
  28. if (s[0] == '-') { a+= 5; }
  29. if (s.length() > 1 && s[1] == '-') { a+= 10; }
  30. if (s.length() > 2 && s[2] == '-') { a+= 20; }
  31. if (s.length() > 3 && s[3] == '-') { a+= 40; }
  32. return a;
  33. }
  34.  
  35. void addMorse(char c, string s)
  36. {
  37. toMorse[(int)c] = s;
  38. fromMorse[tobinary(s)] = c;
  39. }
  40.  
  41. int main() {
  42. // while (scanf("%d%d",) != EOF)
  43. // Morse code
  44. addMorse('A', ".-");
  45. addMorse('B', "-...");
  46. addMorse('C', "-.-.");
  47. addMorse('D', "-..");
  48. addMorse('E', ".");
  49. addMorse('F', "..-.");
  50. addMorse('G', "--.");
  51. addMorse('H', "....");
  52. addMorse('I', "..");
  53. addMorse('J', ".---");
  54. addMorse('K', "-.-");
  55. addMorse('L', ".-..");
  56. addMorse('M', "--");
  57. addMorse('N', "-.");
  58. addMorse('O', "---");
  59. addMorse('P', ".--.");
  60. addMorse('Q', "--.-");
  61. addMorse('R', ".-.");
  62. addMorse('S', "...");
  63. addMorse('T', "-");
  64. addMorse('U', "..-");
  65. addMorse('V', "...-");
  66. addMorse('W', ".--");
  67. addMorse('X', "-..-");
  68. addMorse('Y', "-.--");
  69. addMorse('Z', "--..");
  70. addMorse('_', "..--");
  71. addMorse(',', ".-.-");
  72. addMorse('.', "---.");
  73. addMorse('?', "----");
  74.  
  75. string input;
  76. char morse[4000];
  77. int counts[1000];
  78. while (getline(cin, input, '\n'))
  79. {
  80. int count = 0;
  81. count = input.length();
  82. int curmorse = 0;
  83. for (int i = 0; i < count; i++)
  84. {
  85. string morseChar = toMorse[(int)input[i]];
  86. counts[i] = morseChar.length();
  87. for (int j =0; j < counts[i];j++)
  88. {
  89. morse[curmorse] = morseChar[j];
  90. curmorse++;
  91. }
  92. }
  93. // Loaded to stage 2.
  94. int printwhat = 0;
  95. string morseString(morse);
  96. for (int i = 0; i < count; i++)
  97. {
  98. printf("%c",
  99. fromMorse[
  100. tobinary(
  101. morseString.substr(printwhat, counts[count-i-1]).c_str())]);
  102.  
  103. printwhat +=counts[count-i-1];
  104. }
  105. printf("\n");
  106. }
  107.  
  108. return 0;
  109. }