Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. function normalize($first, $second) {
  4.     $diff = array_sum($first) - array_sum($second);
  5.  
  6.     list($first, $second) = $diff > 0 ? [$second, $first] : [$first, $second];
  7.  
  8.     $diff = abs($diff / 2);
  9.  
  10.     $maxChar = null;
  11.     $maxCount = 0;
  12.  
  13.     foreach ($second as $char => $count) {
  14.         if ($count <= $diff) {
  15.             if ($count > $maxCount) {
  16.                 $maxCount = $count;
  17.                 $maxChar = $char;
  18.             }
  19.         }
  20.     }
  21.  
  22.     if ($maxChar === null) {
  23.         return [$first, $second, false];
  24.     }
  25.  
  26.     $first[$maxChar] = $maxCount;
  27.  
  28.     unset($second[$maxChar]);
  29.  
  30.     return [$first, $second, true];
  31. }
  32.  
  33. function divide_array($array) {
  34.     $first = [];
  35.     $second = [];
  36.  
  37.     $index = 0;
  38.     $firstSum = 0;
  39.  
  40.     foreach ($array as $key => $value) {
  41.         if (($firstSum + $value) > array_sum(array_slice($array, $index + 1))) {
  42.             $second = array_splice($array, $index);
  43.  
  44.             break;
  45.         }
  46.  
  47.         $first[$key] = $value;
  48.         $firstSum += $value;
  49.  
  50.         $index++;
  51.     }
  52.  
  53.     $needNormalization = true;
  54.  
  55.     while ($needNormalization) {
  56.         list($first, $second, $needNormalization) = normalize($first, $second);
  57.     }
  58.  
  59.     return [$first, $second];
  60. }
  61.  
  62. function encode_alphabet($alphabet) {
  63.     list($first, $second) = divide_array($alphabet);
  64.  
  65.     if (count($first) === 1) {
  66.         $key = key($first);
  67.  
  68.         $firstCodes = [$key => '0'];
  69.     } else {
  70.         $firstCodes = [];
  71.  
  72.         foreach (encode_alphabet($first) as $char => $code) {
  73.             $firstCodes[$char] = '0'.$code;
  74.         }
  75.     }
  76.  
  77.     if (count($second) === 1) {
  78.         $key = key($second);
  79.  
  80.         $secondCodes = [$key => '1'];
  81.     } else {
  82.         $secondCodes = [];
  83.  
  84.         foreach (encode_alphabet($second) as $char => $code) {
  85.             $secondCodes[$char] = '1'.$code;
  86.         }
  87.     }
  88.  
  89.     return array_merge($firstCodes, $secondCodes);
  90. }
  91.  
  92. function encode_string($string)
  93. {
  94.     $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
  95.     $alphabet = array_count_values($chars);
  96.  
  97.     $codes = encode_alphabet($alphabet);
  98.  
  99.     $encodedString = str_replace(array_keys($codes), array_values($codes), $string);
  100.  
  101.     return [$codes, $encodedString];
  102. }
  103.  
  104. function decode_string($encodedString, $codes) {
  105.     uasort($codes, function ($a, $b){
  106.         return mb_strlen($b) - mb_strlen($a);
  107.     });
  108.  
  109.     $decodedString = '';
  110.  
  111.     while ($encodedString !== '') {
  112.         foreach ($codes as $char => $code) {
  113.             $len = mb_strlen($code);
  114.  
  115.             if ($len > mb_strlen($encodedString)) {
  116.                 continue;
  117.             }
  118.  
  119.             if (mb_substr($encodedString, 0, $len) === $code) {
  120.                 $decodedString .= $char;
  121.                 $encodedString = mb_substr($encodedString, $len);
  122.  
  123.                 break;
  124.             }
  125.         }
  126.     }
  127.  
  128.     return $decodedString;
  129. }
  130.  
  131. $string = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi perferendis dicta, atque veniam est labore.';
  132.  
  133. list($codes, $encodedString) = encode_string($string);
  134.  
  135. $decodedString = decode_string($encodedString, $codes);
  136.  
  137. echo $string === $decodedString ? "Equals" : "Not Equals", PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement