Source code for submission s523

fs.cpp

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string encode( char s, int & len )
  6. {
  7. string pole[26] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
  8. ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
  9. "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
  10.  
  11. len = 4;
  12. if( s == '_' )return "..--";
  13. if( s == ',' ) return ".-.-";
  14. if( s == '.' ) return "---.";
  15. if( s == '?' ) return "----";
  16.  
  17. len = pole[ s - 'A' ].length();
  18. return pole[ s - 'A' ];
  19. }
  20.  
  21. char decode( const string & s )
  22. {
  23. string pole[26] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
  24. ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
  25. "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
  26.  
  27. int i;
  28. for( i = 0; i < 26; i++ )
  29. if( s == pole[i] )
  30. return 'A' + i;
  31.  
  32. if( s == "..--" ) return '_';
  33. if( s == ".-.-" ) return ',';
  34. if( s == "---." ) return '.';
  35. if( s == "----" ) return '?';
  36. return 0;
  37. }
  38.  
  39. int main()
  40. {
  41. string s;
  42. while( getline(cin, s) )
  43. {
  44. string res;
  45. int len = s.length();
  46. int lenArr[len];
  47.  
  48. for( int i = 0; i < len; i++ )
  49. res += encode( s[i], lenArr[i] );
  50.  
  51. int pos = 0;
  52. for( int i = len-1; i >= 0; i-- )
  53. {
  54. string c = res.substr( pos, lenArr[i] );
  55. pos += lenArr[i];
  56. cout << decode( c );
  57. }
  58. cout << "\n";
  59.  
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. /**
  66.  
  67. AKADTOF_IBOETATUK_IJN
  68. FENDSVTSLHW.EDATS,EULAY
  69. TRDNWPLOEF
  70. NTTTGAZEJUIIGDUZEHKUE
  71. QEWOISE.EIVCAEFNRXTBELYTGD.
  72. ?EJHUT.TSMYGW?EJHOT
  73. DSU.XFNCJEVE.OE_UJDXNO_YHU?VIDWDHPDJIKXZT?E
  74. ADAWEKHZN,OTEATWRZMZN_IDWCZGTEPION
  75.  
  76.  
  77. */
  78.