Advertisement
Guest User

Untitled

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