Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DecoderFactory {
- private $minlen = 0;
- private $maxlen = 0;
- private $line = "";
- private $linelength = 0;
- private $patterns = array();
- public function __construct($minlen,$maxlen) {
- $this->minlen = $minlen;
- $this->maxlen = $maxlen;
- }
- public function load($msg) {
- foreach (str_split($msg) as $m) $this->push($m);
- }
- public function getPatterns() {
- return $this->patterns;
- }
- public function push($char) {
- $l = strlen($char);
- if ($l == 0) return;
- if ($l != 1) $char = $char[0];
- $this->line .= $char;
- $this->linelength ++;
- for ($n=$this->minlen; $n <= $this->maxlen; $n++) {
- if ($n > $this->linelength) break;
- $key = substr($this->line,-$n);
- if (!isset($this->patterns[$key]))
- $this->patterns[$key] = 1;
- else
- $this->patterns[$key]++;
- }
- }
- }
- // --------------
- if ($argc != 5) die ("uso: $argv[0] minlen maxlen num_rank mensaje\n");
- $patrones = array();
- $minlen = intval($argv[1]);
- $maxlen = intval($argv[2]);
- $numrank = intval($argv[3]);
- $mensaje = $argv[4];
- if ($minlen < 1) die ("minlen incorrecto\n");
- if ($maxlen < $minlen) die ("maxlen incorrecto\n");
- if ($maxlen > strlen($mensaje) || $numrank < 1) die ("no hay patrones\n");
- $decoder = new DecoderFactory($minlen,$maxlen);
- $decoder->load($mensaje);
- $patrones = $decoder->getPatterns();
- arsort($patrones,SORT_NUMERIC);
- $tally = 0;
- foreach ($patrones as $patron => $num) {
- echo "$num\t$patron\n";
- if (++$tally == $numrank) break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement