Advertisement
Ksenia_C

Untitled

Oct 8th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. namespace solve1 {
  7.  
  8. int Kn, Km;
  9. string bin(int x, int y) {
  10. string w = "";
  11. for (int i = 0; i < Kn; i++) {
  12. w += (char)(x % 2 + '0');
  13. x >>= 1;
  14. }
  15. for (int i = 0; i < Km; i++) {
  16. w += (char)(y % 2 + '0');
  17. y >>= 1;
  18. }
  19. return w;
  20. }
  21.  
  22. pair<int, int> nbin(const vector<char>& s, int& i) {
  23. int x = 0, y = 0;
  24. for (int j = i + Km + Kn - 1; j >= i + Kn; j--)
  25. y = 2 * y + (s[j] - '0');
  26. for (int j = i + Kn - 1; j >= i; j--)
  27. x = 2 * x + (s[j] - '0');
  28. i += Kn + Km;
  29. return { x, y };
  30. }
  31. int sum(vector<vector<int>>& pref, int x, int y, int x2, int y2) {
  32. if (x == 0 && y == 0) {
  33. return pref[x2][y2];
  34. }
  35. if (x == 0) {
  36. return pref[x2][y2] - pref[x2][y - 1];
  37. }
  38. if (y == 0) {
  39. return pref[x2][y2] - pref[x - 1][y2];
  40. }
  41. return pref[x2][y2] - pref[x - 1][y2] - pref[x2][y - 1] + pref[x - 1][y - 1];
  42. }
  43.  
  44. string insert(vector<vector<char>>& a, vector<vector<int>>& pref, int x, int y) {
  45. int x2 = x, y2 = y;
  46. x2++;
  47. while (x2 < a.size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
  48. ++x2;
  49. }
  50. --x2;
  51. y2++;
  52. while (y2 < a[0].size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
  53. ++y2;
  54. }
  55. --y2;
  56. string an = "";
  57. for (int i = x; i <= x2; ++i) {
  58. for (int j = y; j <= y2; ++j) {
  59. a[i][j] = '2';
  60. }
  61. }
  62. an = bin(x, y) + bin(x2, y2);
  63. return an;
  64. }
  65.  
  66. string decode(vector<vector<char>>& a) {
  67. string ans = "";
  68. int n = a.size();
  69. int m = a[0].size();
  70. vector<vector<int>> pref_sum(n, vector<int>(m));
  71. for (int i = 0; i < n; ++i) {
  72. for (int j = 0; j < m; ++j) {
  73. pref_sum[i][j] = a[i][j] - '0';
  74. if (0 < i && 0 < j) {
  75. pref_sum[i][j] += pref_sum[i - 1][j] + pref_sum[i][j - 1] - pref_sum[i - 1][j - 1];
  76. }
  77. if (0 < i && j == 0) {
  78. pref_sum[i][j] += pref_sum[i - 1][j];
  79. }
  80. if (i == 0 && 0 < j) {
  81. pref_sum[i][j] += pref_sum[i][j - 1];
  82. }
  83. }
  84. }
  85. for (int j = 0; j < m; ++j) {
  86. for (int i = 0; i < n; ++i) {
  87. if (a[i][j] == '1') {
  88. ans += insert(a, pref_sum, i, j);
  89. }
  90. }
  91. }
  92. return ans;
  93. }
  94.  
  95. void pol(vector<vector<char>>& a, int x, int y, int x2, int y2) {
  96. for (int i = x; i <= x2; ++i) {
  97. for (int j = y; j <= y2; ++j) {
  98. a[i][j] = '1';
  99. }
  100. }
  101. }
  102.  
  103. string main(string t) {
  104. if (t == "encode") {
  105. int n, m, k;
  106. cin >> n >> m >> k;
  107. for (; (1 << Kn) < n; ++Kn);
  108. for (; (1 << Km) < m; ++Km);
  109. int pKn = Kn, pKm = Km;
  110. Kn = Km = 4;
  111. cout << bin(pKn, pKm);
  112. Kn = pKn; Km = pKm;
  113. cout << bin(n, m);
  114. vector<vector<char>> a(n, vector<char>(m));
  115. for (int i = 0; i < n; ++i) {
  116. for (int j = 0; j < m; ++j) {
  117. cin >> a[i][j];
  118. }
  119. }
  120. return "0" + decode(a);
  121. } else {
  122. vector<char> a;
  123. char c;
  124. while (cin >> c) {
  125. a.push_back(c);
  126. }
  127. Kn = Km = 4;
  128. int p = 0;
  129. pair<int, int> gg = nbin(a, p);
  130. Kn = gg.first; Km = gg.second;
  131. int n = 0, m = 0;
  132. pair<int, int> t = nbin(a, p);
  133. n = t.first;
  134. m = t.second;
  135. vector<vector<char>> ans(n, vector<char>(m, '0'));
  136. while (p < a.size()) {
  137. int x = 0, y = 0, x2 = 0, y2 = 0;
  138. pair<int, int> t2 = nbin(a, p);
  139. x = t2.first;
  140. y = t2.second;
  141. pair<int, int> t3 = nbin(a, p);
  142. x2 = t3.first;
  143. y2 = t3.second;
  144. pol(ans, x, y, x2, y2);
  145. }
  146. for (int i = 0; i < n; ++i) {
  147. for (int j = 0; j < m; ++j) {
  148. cout << ans[i][j];
  149. }
  150. cout << "\n";
  151. }
  152. return "";
  153. }
  154. }
  155. }
  156. namespace solve2 {
  157. string bin(int x) {
  158. string s;
  159. for (int i = 0; i < 10; ++i) {
  160. s += '0' + x % 2;
  161. x /= 2;
  162. }
  163. reverse(s.begin(), s.end());
  164. return s;
  165. }
  166.  
  167. int sum(vector<vector<int>>& pref, int x, int y, int x2, int y2) {
  168. if (x == 0 && y == 0) {
  169. return pref[x2][y2];
  170. }
  171. if (x == 0) {
  172. return pref[x2][y2] - pref[x2][y - 1];
  173. }
  174. if (y == 0) {
  175. return pref[x2][y2] - pref[x - 1][y2];
  176. }
  177. return pref[x2][y2] - pref[x - 1][y2] - pref[x2][y - 1] + pref[x - 1][y - 1];
  178. }
  179.  
  180. string insert(vector<vector<char>>& a, vector<vector<int>>& pref, int x, int y) {
  181. int x2 = x, y2 = y;
  182. x2++;
  183. while (x2 < a.size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
  184. ++x2;
  185. }
  186. --x2;
  187. y2++;
  188. while (y2 < a[0].size() && sum(pref, x, y, x2, y2) == (x2 - x + 1) * (y2 - y + 1)) {
  189. ++y2;
  190. }
  191. --y2;
  192. string an = "";
  193. for (int i = x; i <= x2; ++i) {
  194. for (int j = y; j <= y2; ++j) {
  195. a[i][j] = '2';
  196. }
  197. }
  198. an = bin(x) + bin(y) + bin(x2) + bin(y2);
  199. return an;
  200. }
  201.  
  202. string decode(vector<vector<char>>& a) {
  203. string ans = "";
  204. int n = a.size();
  205. int m = a[0].size();
  206. vector<vector<int>> pref_sum(n, vector<int>(m));
  207. for (int i = 0; i < n; ++i) {
  208. for (int j = 0; j < m; ++j) {
  209. pref_sum[i][j] = a[i][j] - '0';
  210. if (0 < i && 0 < j) {
  211. pref_sum[i][j] += pref_sum[i - 1][j] + pref_sum[i][j - 1] - pref_sum[i - 1][j - 1];
  212. }
  213. if (0 < i && j == 0) {
  214. pref_sum[i][j] += pref_sum[i - 1][j];
  215. }
  216. if (i == 0 && 0 < j) {
  217. pref_sum[i][j] += pref_sum[i][j - 1];
  218. }
  219. }
  220. }
  221. for (int j = 0; j < m; ++j) {
  222. for (int i = 0; i < n; ++i) {
  223. if (a[i][j] == '1') {
  224. ans += insert(a, pref_sum, i, j);
  225. }
  226. }
  227. }
  228. return ans;
  229. }
  230.  
  231. void pol(vector<vector<char>>& a, int x, int y, int x2, int y2) {
  232. for (int i = x; i <= x2; ++i) {
  233. for (int j = y; j <= y2; ++j) {
  234. a[i][j] = '1';
  235. }
  236. }
  237. }
  238.  
  239. string main(string t) {
  240. if (t == "encode") {
  241. int n, m, k;
  242. cin >> n >> m >> k;
  243. cout << bin(n) << bin(m);
  244. vector<vector<char>> a(n, vector<char>(m));
  245. for (int i = 0; i < n; ++i) {
  246. for (int j = 0; j < m; ++j) {
  247. cin >> a[i][j];
  248. }
  249. }
  250. return "1" + decode(a);
  251. } else {
  252. vector<char> a;
  253. char c;
  254. while (cin >> c) {
  255. a.push_back(c);
  256. }
  257. int n = 0, m = 0;
  258. int p = 0;
  259. for (int i = 0; i < 10; ++i) n = n * 2 + (a[p++] - '0');
  260. for (int i = 0; i < 10; ++i) m = m * 2 + (a[p++] - '0');
  261. vector<vector<char>> ans(n, vector<char>(m, '0'));
  262. while (p < a.size()) {
  263. int x = 0, y = 0, x2 = 0, y2 = 0;
  264. for (int i = 0; i < 10; ++i) x = x * 2 + (a[p++] - '0');
  265. for (int i = 0; i < 10; ++i) y = y * 2 + (a[p++] - '0');
  266. for (int i = 0; i < 10; ++i) x2 = x2 * 2 + (a[p++] - '0');
  267. for (int i = 0; i < 10; ++i) y2 = y2 * 2 + (a[p++] - '0');
  268. pol(ans, x, y, x2, y2);
  269. }
  270. for (int i = 0; i < n; ++i) {
  271. for (int j = 0; j < m; ++j) {
  272. cout << ans[i][j];
  273. }
  274. cout << "\n";
  275. }
  276. return "";
  277. }
  278. }
  279. }
  280.  
  281. int main() {
  282. string t; cin >> t;
  283. if (t == "encode") {
  284. auto s1 = solve1::main(t);
  285. auto s2 = solve2::main(t);
  286. if (s1.size() << s2.size()) {
  287. cout << s1;
  288. } else {
  289. cout << s2;
  290. }
  291. } else {
  292. char c; cin >> c;
  293. if (c == '0') {
  294. solve1::main(t);
  295. } else {
  296. solve2::main(t);
  297. }
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement