Advertisement
Tahsin24

uva - 213

Oct 24th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. verdict : Run Time Error
  3. */
  4. #include <iostream>
  5. #include <cstring>
  6. #include <string>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <algorithm>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. int binary_decimal(string a)
  16. {
  17. int d = 0;
  18. int j = 0;
  19. for (int i = a.length() - 1; i >= 0; i--)
  20. {
  21. int k = j;
  22. int m = (a[i] - '0');
  23. while (k--)
  24. {
  25. m *= 2;
  26. }
  27. d += m;
  28. j++;
  29. }
  30. return d;
  31. }
  32.  
  33. string decimal_binary(int a)
  34. {
  35. string s = "";
  36. char t[1000000];
  37. int i = 0;
  38. int r = 0;
  39. while (1)
  40. {
  41. if (a == 0)break;
  42. r = a % 2;
  43. t[i] = char(r+48);
  44. i++;
  45. a = a / 2;
  46. }
  47. t[i] = '\0';
  48. s = t;
  49. reverse(s.begin(), s.end());
  50. return s;
  51. }
  52.  
  53. string all_one(int d)
  54. {
  55. string s = "";
  56. while (d--)
  57. {
  58. s.append("1");
  59. }
  60. return s;
  61. }
  62.  
  63. string decode(string header, string x)
  64. {
  65.  
  66. int d = binary_decimal(x);
  67. int l = x.length() - 1;
  68.  
  69. int k = 1;
  70.  
  71. while (l--)
  72. {
  73. k *= 2;
  74. }
  75. d += k;
  76. if (x == "0")d = 0;
  77. if (x == "00")d = 1;
  78. if (x == "01")d = 2;
  79. if (x == "10")d = 3;
  80. char a[2];
  81. a[0] = header[d];
  82. a[1] = '\0';
  83. string s = a;
  84. return s;
  85.  
  86. }
  87.  
  88. int main()
  89. {
  90. string header;
  91.  
  92. while (getline(cin, header))
  93. {
  94.  
  95. string message = "";
  96. string a;
  97. char filter[1000000];
  98. filter[0] = '\0';
  99. while (1)
  100. {
  101. getline(cin, a);
  102. int j = 0;
  103. for (int i = 0; i < a.size(); i++)
  104. {
  105. if (a[i] != char(13))
  106. {
  107. filter[j] = a[i];
  108. j++;
  109. }
  110. }
  111. filter[j] = '\0';
  112. a = filter;
  113. message.append(a);
  114. if (a[a.length() - 1] == '0' && a[a.length() - 2] == '0' && a[a.length() - 3] == '0')
  115. {
  116. break;
  117. }
  118. }
  119. string result = "";
  120.  
  121. a = message.substr(0, 3);
  122.  
  123. int d = binary_decimal(a);
  124.  
  125.  
  126.  
  127. int k = 3;
  128.  
  129. string end = all_one(d);
  130.  
  131.  
  132. string t = message.substr(k, d);
  133.  
  134. while (1)
  135. {
  136.  
  137. while (1)
  138. {
  139. if (t == end)break;
  140. result.append(decode(header, t));
  141. k += d;
  142. t = message.substr(k, d);
  143. }
  144.  
  145. k += d;
  146. a = message.substr(k, 3);
  147. if (a == "000")break;
  148. k += 3;
  149. d = binary_decimal(a);
  150. end = all_one(d);
  151. t = message.substr(k, d);
  152. }
  153.  
  154.  
  155. cout << result << endl;
  156.  
  157. message = "";
  158.  
  159. result = "";
  160.  
  161. a = "";
  162.  
  163. t = "";
  164.  
  165. end = "";
  166.  
  167. filter[0] = '\0';
  168. d = k = 0;
  169. header = "";
  170. }
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement