Source code for submission s500

fs.cpp

  1. #include <stdio.h>
  2. #include <map>
  3. #include <string>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. string mm[31] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
  11. "....", "..", ".---", "-.-", ".-..", "--", "-.",
  12. "---", ".--.", "--.-", ".-.", "...", "-", "..-",
  13. "...-", ".--", "-..-", "-.--", "--..",
  14. "..--", ".-.-", "---.", "----"
  15. };
  16.  
  17.  
  18. map<string, int> mr;
  19.  
  20. int main() {
  21.  
  22. for ( int i = 0; i < 30; ++i )
  23. mr[string(mm[i])] = i;
  24.  
  25. string str;
  26.  
  27. while ( getline(cin, str) ) {
  28.  
  29. string tmp;
  30. vector <int> tmp2(str.size());
  31.  
  32. for ( int i = 0; i < str.size(); ++i ) {
  33.  
  34. int p;
  35.  
  36. if ( str[i] >= 'A' && str[i] <= 'Z' )
  37. p = str[i] - 'A';
  38. else if ( str[i] == '_' )
  39. p = 26;
  40. else if ( str[i] == ',' )
  41. p = 27;
  42. else if ( str[i] == '.' )
  43. p = 28;
  44. else if ( str[i] == '?' )
  45. p = 29;
  46. else {
  47. printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAArgh....\n");
  48. return 1;
  49. }
  50.  
  51. tmp += mm[p];
  52. tmp2[i] = mm[p].size();
  53.  
  54. }
  55.  
  56. // cout << tmp << endl;
  57.  
  58. int last = 0;
  59. int i = tmp2.size()-1;
  60.  
  61. while ( last < tmp.size() ) {
  62.  
  63. char str2[10];
  64. strncpy(str2, tmp.c_str()+last, tmp2[i]);
  65. str2[tmp2[i]] = 0;
  66.  
  67. int c = mr[string(str2)];
  68.  
  69. // printf("str2=%s c=%d tmp2[i]=%d\n", str2, c, tmp2[i]);
  70.  
  71. if ( c < 26 )
  72. putchar(c+'A');
  73. else if ( c == 26 )
  74. putchar('_');
  75. else if ( c == 27 )
  76. putchar(',');
  77. else if ( c == 28 )
  78. putchar('.');
  79. else if ( c == 29 )
  80. putchar('?');
  81.  
  82. last += tmp2[i];
  83. --i;
  84.  
  85. }
  86.  
  87. putchar('\n');
  88.  
  89. }
  90.  
  91. return 0;
  92.  
  93. }
  94.