Advertisement
Mihail_Atnsv

kaminoFactory

Oct 15th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Toshiba
  5.  * Date: 12.10.2018 г.
  6.  * Time: 11:54
  7.  */
  8. $lengthOfSequence = intval(readline());
  9. $sequence = readline();
  10. $currentDna = [];
  11. $bestSample = [];
  12.  
  13. while ($sequence != "Clone them!") {
  14.  
  15.     $lineDna = preg_replace("/[^0-9]/", "", $sequence);
  16.     $lineDna = str_split($lineDna);
  17.  
  18.     if (count($lineDna) == $lengthOfSequence) {
  19.         array_push($currentDna, $lineDna);
  20.     }
  21.  
  22.     $sequence = readline();
  23. }
  24.  
  25. $index = 1;
  26. $bestStartIndex = $lengthOfSequence;
  27. $bestLength = 0;
  28. $bestSum = 0;
  29.  
  30. $bestSample = $currentDna[0];
  31. $sum = 0;
  32.  
  33. for ($i = 0; $i < count($currentDna); $i++) {
  34.     $sum = array_sum($currentDna[$i]);
  35.     $startIndex = $lengthOfSequence;
  36.     $bestLen = 0;
  37.     $len = 0;
  38.  
  39.     for ($j = 0; $j < count($currentDna[$i]); $j++) {
  40.  
  41.         if ($currentDna[$i][$j] == 1) {
  42.             $len++;
  43.  
  44.             if ($j == count($currentDna[$i]) - 1) {
  45.  
  46.                 if ($len > $bestLen) {
  47.  
  48.                     $bestLen = $len;
  49.                     $startIndex = $j - $len;
  50.                 }
  51.                 $len = 0;
  52.             }
  53.         } else {
  54.  
  55.             if ($len > $bestLen) {
  56.  
  57.                 $bestLen = $len;
  58.                 $startIndex = $j - $len;
  59.             }
  60.             $len = 0;
  61.         }
  62.     }
  63.     if ($bestLen > $bestLength) {
  64.         $bestLength = $bestLen;
  65.         $index = $i + 1;
  66.         $bestSum = $sum;
  67.         $bestStartIndex = $startIndex;
  68.         $bestSample = $currentDna[$i];
  69.     } elseif ($bestLen == $bestLength) {
  70.  
  71.         if ($bestStartIndex > $startIndex) {
  72.             $index = $i + 1;
  73.             $bestSum = $sum;
  74.             $bestStartIndex = $startIndex;
  75.             $bestSample = $currentDna[$i];
  76.         } elseif ($bestStartIndex == $startIndex) {
  77.             if ($bestSum < $sum) {
  78.                 $index = $i + 1;
  79.                 $bestSum = $sum;
  80.                 $bestSample = $currentDna[$i];
  81.             }
  82.         }
  83.     }
  84. }
  85. echo "Best DNA sample $index with sum: $bestSum." . PHP_EOL;
  86. foreach ($bestSample as $dna) {
  87.     echo $dna . " ";
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement