Guest User

Untitled

a guest
Oct 21st, 2014
180
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class DecoderLine {
  4.  
  5.     private $length = 0;
  6.  
  7.     private $ring = "";
  8.     private $ringlength = 0;
  9.    
  10.     private $patterns = array();
  11.  
  12.     public function __construct($len) {
  13.         $this->length = $len;
  14.     }
  15.    
  16.     public function load($msg) {
  17.         foreach (str_split($msg) as $m) $this->push($m);
  18.     }
  19.    
  20.     public function getPatterns() {
  21.         return $this->patterns;
  22.     }
  23.    
  24.     public function push($char) {
  25.         $l = strlen($char);
  26.         if ($l == 0) return;
  27.         if ($l != 1) $char = $char[0];
  28.        
  29.         $this->ring .= $char;
  30.  
  31.         if ($this->ringlength < $this->length) {
  32.             $this->ringlength++;
  33.             if ($this->ringlength < $this->length) return;
  34.         } else {
  35.             $this->ring = substr($this->ring,1);
  36.         }
  37.        
  38.         $key = "_" .$this->ring; // evita bug / comportamiento indocumentado de array_merge
  39.        
  40.         if (!isset($this->patterns[$key]))
  41.             $this->patterns[$key] = 1;
  42.         else
  43.             $this->patterns[$key]++;
  44.     }
  45. }
  46.  
  47. // --------------
  48.  
  49. if ($argc != 5) die ("uso: $argv[0] minlen maxlen num_rank mensaje\n");
  50.  
  51. $patrones = array();
  52.  
  53. $minlen = intval($argv[1]);
  54. $maxlen = intval($argv[2]);
  55. $numrank = intval($argv[3]);
  56. $mensaje = $argv[4];
  57.  
  58. if ($minlen < 1) die ("minlen incorrecto\n");
  59. if ($maxlen < $minlen) die ("maxlen incorrecto\n");
  60.  
  61. if ($maxlen > strlen($mensaje) || $numrank < 1) die ("no hay patrones\n");
  62.  
  63. for ($n = $minlen; $n <= $maxlen; $n++) {
  64.     $decoder = new DecoderLine($n);
  65.     $decoder->load($mensaje);
  66.     $patrones = array_merge($patrones,$decoder->getPatterns());
  67. }
  68.  
  69. arsort($patrones,SORT_NUMERIC);
  70.  
  71. $tally = 0;
  72.  
  73. foreach ($patrones as $patron => $num) {
  74.     $p = substr($patron,1);
  75.     echo "$num\t$p\n";
  76.     if (++$tally == $numrank) break;
  77. }
RAW Paste Data