Advertisement
Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. ## STOKROTKI ##
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int t;
  10. cin >> t;
  11. while (t-- > 0) {
  12. int w, k;
  13. cin>> w >> k;
  14. int a[w][k];
  15. for (int i = 0; i < w; i++)
  16. for (int j = 0; j < k; j++)
  17. cin >> a[i][j];
  18. ;
  19. int c[w][k];
  20. c[0][0] = a[0][0];
  21. for (int i = 1; i < w; i++)
  22. c[i][0] = a[i][0] + c[i-1][0];
  23. for (int j = 1; j < k; j++)
  24. c[0][j] = a[0][j] + c[0][j-1];
  25. for (int i = 1; i < w; i++)
  26. for (int j = 1; j < k; j++)
  27. c[i][j] = a[i][j] + max(c[i-1][j], c[i][j-1]);
  28. ;
  29.  
  30. cout << c[w-1][k-1] << endl;
  31.  
  32.  
  33. }
  34.  
  35.  
  36. return 0;
  37. }
  38.  
  39.  
  40. ## NAJDLUZSZA WYMAZANKA
  41.  
  42. #include <iostream>
  43. #include <vector>
  44. #include <list>
  45. #include <string>
  46. #include <limits>
  47.  
  48. #define DEBUG 0
  49.  
  50.  
  51. class LCS
  52. {
  53. std::string u;
  54. std::string v;
  55.  
  56. std::vector<std::vector<unsigned short> > lengths;
  57.  
  58. bool need_predecessors;
  59.  
  60. enum direction
  61. {
  62. NONE,
  63. UP,
  64. LEFT,
  65. DIAGONAL
  66. };
  67.  
  68. std::vector<std::vector<direction> > directions;
  69.  
  70. void init()
  71. {
  72. lengths.resize(u.length() + 1);
  73.  
  74. lengths[0].resize(v.length() + 1, 0);
  75.  
  76. for(std::vector<std::vector<unsigned short> >::iterator it = lengths.begin() + 1; it != lengths.end(); ++it)
  77. {
  78. it->resize(v.length() + 1);
  79. (*it)[0] = 0;
  80. }
  81.  
  82. if (need_predecessors)
  83. {
  84. directions.resize(u.length() + 1);
  85. for(std::vector<std::vector<direction> >::iterator it = directions.begin(); it != directions.end(); ++it)
  86. {
  87. it->resize(v.length() + 1);
  88. }
  89.  
  90. for (unsigned short i = 0; i <= u.length(); ++i)
  91. {
  92. directions[i][0] = NONE;
  93. }
  94. for (unsigned short i = 1; i <= v.length(); ++i)
  95. {
  96. directions[0][i] = NONE;
  97. }
  98. }
  99. }
  100.  
  101. void set_length_and_path_from_predecessors(const unsigned short col, const unsigned short row)
  102. {
  103. const char letter_u = u[col - 1];
  104. const char letter_v = v[row - 1];
  105.  
  106. if (letter_u == letter_v)
  107. {
  108. if (need_predecessors) directions[col][row] = DIAGONAL;
  109. lengths[col][row] = 1 + lengths[col - 1][row - 1];
  110. }
  111. else if (lengths[col][row - 1] >= lengths[col - 1][row])
  112. {
  113. if (need_predecessors) directions[col][row] = UP;
  114. lengths[col][row] = lengths[col][row - 1];
  115. }
  116. else
  117. {
  118. if (need_predecessors) directions[col][row] = LEFT;
  119. lengths[col][row] = lengths[col - 1][row];
  120. }
  121. }
  122.  
  123. public:
  124. enum str_id
  125. {
  126. U,
  127. V
  128. };
  129.  
  130. void read_line(str_id s)
  131. {
  132. switch (s)
  133. {
  134. case U:
  135. std::cin >> u;
  136. break;
  137.  
  138. case V:
  139. std::cin >> v;
  140. break;
  141. }
  142. }
  143.  
  144.  
  145. void run(bool predecessors = true)
  146. {
  147. need_predecessors = predecessors;
  148. init();
  149.  
  150. for (unsigned short row = 1; row <= v.length(); ++row)
  151. {
  152. for (unsigned short col = 1; col <= u.length(); ++col)
  153. {
  154. set_length_and_path_from_predecessors(col, row);
  155. }
  156. }
  157.  
  158. }
  159.  
  160.  
  161. unsigned short longest_length()
  162. {
  163. return lengths[u.length()][v.length()];
  164. }
  165.  
  166. void clear()
  167. {
  168. u.clear();
  169. v.clear();
  170. lengths.resize(0);
  171. directions.resize(0);
  172. }
  173.  
  174. std::string longest_common_subsequence()
  175. {
  176. std::vector<char> s_vec;
  177. s_vec.resize(longest_length());
  178.  
  179. int x = u.length();
  180. int y = v.length();
  181.  
  182. int i = longest_length() - 1;
  183.  
  184. while (i >= 0)
  185. {
  186. switch (directions[x][y])
  187. {
  188. case UP:
  189. --y;
  190. break;
  191.  
  192. case LEFT:
  193. --x;
  194. break;
  195.  
  196. default:
  197. s_vec[i] = u[x-1];
  198. --i;
  199. --x;
  200. --y;
  201. break;
  202. }
  203. }
  204.  
  205. return std::string(s_vec.begin(), s_vec.end());
  206. }
  207.  
  208. void DEBUG_print_strings()
  209. {
  210. if (DEBUG)
  211. {
  212. std::cout << "PRINTING STRINGS" << std::endl
  213. << "U.len=" << u.length() << " with " << u << std::endl
  214. << "V.len=" << v.length() << " with " << v << std::endl;
  215. }
  216. }
  217.  
  218. void DEBUG_print_lengths_contents()
  219. {
  220. if (DEBUG)
  221. {
  222. std::cout << "PRINT LENGTHS CONTENTS:" << std::endl;
  223. for (unsigned short it_v = 0; it_v <= v.length(); ++it_v)
  224. {
  225. for (unsigned short it_u = 0; it_u <= u.length(); ++it_u)
  226. {
  227. std::cout << lengths[it_u][it_v] << "\t";
  228. }
  229. std::cout << std::endl;
  230. }
  231. }
  232. }
  233.  
  234. void DEBUG_print_directions_contents()
  235. {
  236. if (DEBUG)
  237. {
  238. std::cout << "PRINT DIRECTIONS CONTENTS:" << std::endl;
  239. for (unsigned short it_v = 0; it_v <= v.length(); ++it_v)
  240. {
  241. for (unsigned short it_u = 0; it_u <= u.length(); ++it_u)
  242. {
  243. char c = ' ';
  244. switch(directions[it_u][it_v])
  245. {
  246. case NONE:
  247. c = ' ';
  248. break;
  249.  
  250. case UP:
  251. c = '^';
  252. break;
  253.  
  254. case LEFT:
  255. c = '<';
  256. break;
  257.  
  258. case DIAGONAL:
  259. c = '\\';
  260. break;
  261. }
  262. std::cout << c << "\t";
  263. }
  264. std::cout << std::endl;
  265. }
  266. }
  267. }
  268. };
  269.  
  270.  
  271. void omg_just_ignore_this_number_and_go_on()
  272. {
  273. unsigned short x;
  274. std::cin >> x;
  275. //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  276. //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  277. }
  278.  
  279. int main()
  280. {
  281. std::ios_base::sync_with_stdio(false);
  282.  
  283. LCS lcs;
  284.  
  285. lcs.read_line(LCS::U);
  286. lcs.read_line(LCS::V);
  287.  
  288. lcs.DEBUG_print_strings();
  289.  
  290. lcs.run();
  291.  
  292. lcs.DEBUG_print_lengths_contents();
  293. lcs.DEBUG_print_directions_contents();
  294.  
  295. std::cout << lcs.longest_common_subsequence() << std::endl;
  296.  
  297. return 0;
  298. }
  299.  
  300. ## PODCIAG
  301.  
  302. #include <iostream>
  303. #include <string>
  304.  
  305. using namespace std;
  306.  
  307. int main()
  308. {
  309. string s1,s2,sLCS;
  310. int ** L,i,j,m,n;
  311.  
  312. getline(cin,s1);
  313. getline(cin,s2);
  314.  
  315. m = s1.length();
  316. n = s2.length();
  317.  
  318.  
  319.  
  320. L = new int * [m + 1];
  321. for(i = 0; i <= m; i++) L[i] = new int[n + 1];
  322.  
  323.  
  324.  
  325. for(i = 0; i <= m; i++) L[i][0] = 0;
  326. for(j = 0; j <= n; j++) L[0][j] = 0;
  327. for(i = 0; i < m; i++)
  328. for(j = 0; j < n; j++)
  329. if(s1[i] == s2[j])
  330. L[i + 1][j + 1] = 1 + L[i][j];
  331. else
  332. L[i + 1][j + 1] = max(L[i + 1][j],L[i][j + 1]);
  333.  
  334.  
  335.  
  336. sLCS = ""; i = m - 1; j = n - 1;
  337. while((i >= 0) && (j >= 0))
  338. if(s1[i] == s2[j])
  339. {
  340. sLCS = s1[i] + sLCS;
  341. i--; j--;
  342. }
  343. else if(L[i + 1][j] > L[i][j + 1]) j--;
  344. else i--;
  345. cout << L[m][n] << endl;
  346. return 0;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement