Source code for submission s840

morse.cpp

  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8. map<char, string> table;
  9. table['A'] = ".-";
  10. table['B'] = "-...";
  11. table['C'] = "-.-.";
  12. table['D'] = "-..";
  13. table['E'] = ".";
  14. table['F'] = "..-.";
  15. table['G'] = "--.";
  16. table['H'] = "....";
  17. table['I'] = "..";
  18. table['J'] = ".---";
  19. table['K'] = "-.-";
  20. table['L'] = ".-..";
  21. table['M'] = "--";
  22. table['N'] = "-.";
  23. table['O'] = "---";
  24. table['P'] = ".--.";
  25. table['Q'] = "--.-";
  26. table['R'] = ".-.";
  27. table['S'] = "...";
  28. table['T'] = "-";
  29. table['U'] = "..-";
  30. table['V'] = "...-";
  31. table['W'] = ".--";
  32. table['X'] = "-..-";
  33. table['Y'] = "-.--";
  34. table['Z'] = "--..";
  35. table['_'] = "..--";
  36. table['.'] = "---.";
  37. table[','] = ".-.-";
  38. table['?'] = "----";
  39.  
  40. string retezec;
  41. while(getline(cin, retezec)){
  42. string nretezec;
  43. nretezec.resize(retezec.size());
  44.  
  45. int pos = 0;
  46. int len = retezec.size();
  47. int delka_znaku[len];
  48. for(int i = 0; i < len; i++){
  49. string s = table[retezec.at(i)];
  50. nretezec.insert(pos, s);
  51. delka_znaku[i] = s.size();
  52. pos += s.size();
  53. }
  54.  
  55. int reverse[len];
  56. for(int i = len - 1, j = 0; i >= 0; j++, i--){
  57. reverse[j] = delka_znaku[i];
  58. }
  59.  
  60. string result;
  61. pos = 0;
  62. for(int i = 0; i < len; i++){
  63. string part = nretezec.substr(pos, reverse[i]);
  64. map<char, string>::iterator it = table.begin();
  65. for(;it != table.end(); it++){
  66. if(it->second == part){
  67. result.push_back(it->first);
  68. }
  69. }
  70. pos += reverse[i];
  71. }
  72.  
  73. cout << result << endl;
  74. }
  75.  
  76. return 0;
  77. }
  78.