Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2.  
  3. $n = intval(readline());
  4.  
  5. $sequence = explode('!', readline());
  6.  
  7. $p = 1;
  8.  
  9. $bestSequences[$p] = [];
  10. $currentIndex = 0;
  11.  
  12. while($sequence[0] != 'Clone them')
  13. {
  14. $sequences[$p] = [];
  15.  
  16. for ($i = 0; $i < count($sequence); $i++)
  17. {
  18. array_push($sequences[$p], $sequence[$i]);
  19. }
  20. for ($i = 0; $i < count($sequences[$p]); $i++)
  21. {
  22. if ($sequences[$p][$i] == 1)
  23. {
  24. $currentIndex = $i;
  25. $bestSequences[$p][$currentIndex] = 0;
  26. $bestSequences[$p][$currentIndex] ++;
  27. for ($j = $i + 1; $j < count($sequences[$p]); $j++)
  28. {
  29. if ($sequences[$p][$j] == 1)
  30. {
  31. $bestSequences[$p][$currentIndex] ++;
  32. $i++;
  33. }
  34. else
  35. {
  36. break;
  37. }
  38. }
  39. }
  40. }
  41.  
  42. arsort($bestSequences[$p]);
  43. $sequence = explode('!', readline());
  44. $p++;
  45. }
  46.  
  47. for ($i = 1; $i <= count($bestSequences); $i++)
  48. {
  49. $bestSequences[$i]['Sum'] = 0;
  50. foreach ($bestSequences[$i] as $key => $value)
  51. {
  52. $bestSequences[$i]['Sum'] += $value;
  53. }
  54. }
  55.  
  56. uksort($bestSequences, function ($key1, $key2) use ($bestSequences)
  57. {
  58. $v = $bestSequences[$key1][first_key($bestSequences[$key1])] <=> $bestSequences[$key2][first_key($bestSequences[$key2])];
  59. $k = first_key($bestSequences[$key1]) <=> first_key($bestSequences[$key2]);
  60. $s = $bestSequences[$key1]['Sum'] <=> $bestSequences[$key2]['Sum'];
  61.  
  62. if ($v == 1)
  63. {
  64. return $key1 <=> $key2;
  65. }
  66. elseif ($v == -1)
  67. {
  68. return $key2 <=> $key1;
  69. }
  70. elseif ($v == 0)
  71. {
  72. if ($k == 1)
  73. {
  74. return $key2 <=> $key1;
  75. }
  76. elseif ($k == -1)
  77. {
  78. return $key1 <=> $key2;
  79. }
  80. elseif ($k == 0)
  81. {
  82. if ($s == 1)
  83. {
  84. return $key1 <=> $key2;
  85. }
  86. elseif ($s == -1)
  87. {
  88. return $key2 <=> $key1;
  89. }
  90. }
  91. }
  92.  
  93. });
  94.  
  95. $bestSample = first_key($bestSequences);
  96. $sum = $bestSequences[$bestSample]['Sum'];
  97.  
  98. $sample = implode(' ', $sequences[$bestSample]);
  99.  
  100. echo "Best DNA sample {$bestSample} with sum: {$sum}.".PHP_EOL;
  101. echo $sample;
  102.  
  103. function first_key(array $arr)
  104. {
  105. $a = [];
  106. $a = array_keys($arr);
  107. return $a[0];
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement