Source code for submission s562

fs.cpp

  1. #include <cstdio>
  2. #include <vector>
  3. #include <iostream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. string conv[][2] =
  11. {
  12. { "A", "\1\2"},
  13. { "B", "\2\1\1\1"},
  14. { "C", "\2\1\2\1"},
  15. { "D", "\2\1\1"},
  16. { "E", "\1"},
  17. { "F", "\1\1\2\1"},
  18. { "G", "\2\2\1"},
  19. { "H", "\1\1\1\1"},
  20. { "I", "\1\1"},
  21. { "J", "\1\2\2\2"},
  22. { "K", "\2\1\2"},
  23. { "L", "\1\2\1\1"},
  24. { "M", "\2\2"},
  25. { "N", "\2\1"},
  26. { "O", "\2\2\2"},
  27. { "P", "\1\2\2\1"},
  28. { "Q", "\2\2\1\2"},
  29. { "R", "\1\2\1"},
  30. { "S", "\1\1\1"},
  31. { "T", "\2"},
  32. { "U", "\1\1\2"},
  33. { "V", "\1\1\1\2"},
  34. { "W", "\1\2\2"},
  35. { "X", "\2\1\1\2"},
  36. { "Y", "\2\1\2\2"},
  37. { "Z", "\2\2\1\1"},
  38. { "_", "\1\1\2\2"},
  39. { ",", "\1\2\1\2"},
  40. { ".", "\2\2\2\1"},
  41. { "?", "\2\2\2\2"},
  42. };
  43.  
  44. string help(string s)
  45. {
  46. string res;
  47. for (int i = 0; i < s.size(); ++i)
  48. res += s[i] == '\1' ? '.' : '-';
  49. return res;
  50. }
  51.  
  52. bool IsEqual(string const& a, string const& b)
  53. {
  54. return a.size() == b.size() && a == b;
  55. }
  56.  
  57. int main()
  58. {
  59. char arr[1025];
  60. while (scanf("%s\n", arr) > 0)
  61. {
  62. string s(arr);
  63. string mor;
  64. mor.clear();
  65. vector<int> v;
  66. v.clear();
  67. v.resize(s.size(), -1);
  68. //cout << s << endl;
  69. for (int i = 0; i < s.size();)
  70. {
  71. for (int j = 0; j < 30; ++j)
  72. {
  73. // cout << s.substr(i, conv[j][1].size()) << " != " << conv[j][1] << endl;
  74. if (IsEqual(s.substr(i, conv[j][0].size()), conv[j][0]))
  75. {
  76. //cout << s.substr(i, conv[j][0].size()) << " == " << conv[j][0]<< " <- " << help(conv[j][1]) <<endl;
  77. v.push_back(conv[j][1].size());
  78. mor += conv[j][1];
  79. i += conv[j][0].size();
  80. break;
  81. }
  82. }
  83. // cout << "1 " << i << endl;
  84. }
  85.  
  86. // cout << help(mor) << endl;
  87. std::reverse(v.begin(), v.end());
  88.  
  89. string res;
  90. res.clear();
  91. int cnt = 0;
  92. for (int i = 0; i < mor.size();)
  93. {
  94. for (int j = 0; j < 30; ++j)
  95. {
  96. // cout << help(mor.substr(i, v[cnt])) << " != " << help(conv[j][1]) << endl;
  97. if (IsEqual(mor.substr(i, v[cnt]), conv[j][1]))
  98. {
  99. //cout << help(mor.substr(i, v[cnt])) << " != " << help(conv[j][1]) << " ->" << conv[j][0] <<endl;
  100. i += v[cnt];
  101. res += conv[j][0];
  102. ++cnt;
  103. break;
  104. }
  105. }
  106.  
  107. //cout << "1 " << i << endl;
  108. }
  109.  
  110. // cout << cnt << " " << v.size();
  111.  
  112. printf("%s\n", res.c_str());
  113. }
  114.  
  115. return 0;
  116. }
  117.