Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DecoderLine {
- private $length = 0;
- private $ring = "";
- private $ringlength = 0;
- private $patterns = array();
- public function __construct($len) {
- $this->length = $len;
- }
- 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->ring .= $char;
- if ($this->ringlength < $this->length) {
- $this->ringlength++;
- if ($this->ringlength < $this->length) return;
- } else {
- $this->ring = substr($this->ring,1);
- }
- $key = "_" .$this->ring; // evita bug / comportamiento indocumentado de array_merge
- 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");
- for ($n = $minlen; $n <= $maxlen; $n++) {
- $decoder = new DecoderLine($n);
- $decoder->load($mensaje);
- $patrones = array_merge($patrones,$decoder->getPatterns());
- }
- arsort($patrones,SORT_NUMERIC);
- $tally = 0;
- foreach ($patrones as $patron => $num) {
- $p = substr($patron,1);
- echo "$num\t$p\n";
- if (++$tally == $numrank) break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement