2010-04-02 45 views
7

我有这个样子很长整型序列的序列(任意长度!):编码/压缩重复整数

0000000001110002220033333 

现在我需要一些算法,这个字符串转换成一些压缩像

a9b3a3c3a2d5 

这意味着“a 9次,然后b 3次,然后3次”等等,其中“a”代表0,“b”代表1,“c”代表2,“d”代表3。

你会怎么做? 到目前为止,没有什么合适的东西出现在我的脑海里,而且我对谷歌没有好运,因为我真的不知道要搜索什么。这种编码/压缩称为什么?

PS:我打算用PHP进行编码,而在的解码中使用JavaScript

编辑:谢谢大家!

我结束了此功能编码:

protected function numStringToRle($s){   
     $rle = ''; 
     $count = 1; 
     $len = strlen($s); 
     for($i = 0; $i < $len; $i++){ 
      if($i != $len && isset($s[$i+1]) && $s[$i] == $s[$i+1]){ 
       $count++;     
      } else { 
       $rle .= chr($s[$i] + 97).($count == 1 ? '' : $count);         
       $count = 1; 
      } 
     } 
     return $rle;    
} 

而且,对于解码:

var decodeCoords = function(str) { 

    str = str.replace(/(.)(\d+)/g, function(_, x, n) { 
     return new Array(parseInt(n, 10) + 1).join(x); 
    }); 

    return str. 
    replace(/a/g, '0'). 
    replace(/b/g, '1'). 
    replace(/c/g, '2'). 
    replace(/d/g, '3');  
}; 
+1

你到底是用这个做什么?你确定你不能用Gzip压缩它吗? HTTP://计算器。com/questions/294297/javascript-implementation-of-gzip 这将会更加有效的时间和空间明智的,它已经为你完成。 – ryeguy 2010-04-02 13:20:56

+0

gzip不是一个选项,因为我需要用javascript解码。我使用它作为2D游戏的一种掩码。 – Alex 2010-04-02 13:27:46

回答

7

它被称为Run Length Encoding

基本编码器在PHP中:

function numStringToRle($s){ 
    $rle = ''; 
    $count = 1; 
    $len = strlen($s); 
    for ($i = 0; $i < $len; $i++){ 
     if ($i != $len && $s[$i] == $s[$i+1]){ 
      $count++;     
     }else{ 
      $rle .= chr($s[$i] + 97).$count;  
      $count = 1; 
     } 
    } 
    return $rle; 
} 

被警告这将瓶坯不好。如果你将要如何处理可能有很多独立的单个字符,你会得到更好的字符串添加一些复杂性和不写长度与像

123456789123456789 

字符串问题如果运行的长度是1.

//change 
$rle .= chr($s[$i] + 97).$count;  

//to 
$rle .= chr($s[$i] + 97).($count == 1 ? '' : $count); 

//or 
$rle .= chr($s[$i] + 97) 
if ($count != 1){ 
    $rle .= $count; 
} 
+0

工程就像一个魅力,thx! – Alex 2010-04-02 14:19:55

+0

我在找这个算法的名字。谢谢! – Jack 2014-06-11 17:34:41

2

这是一个天真的实现你想要什么。

$toEncode = '0000000001110002220033333'; 
$currentChar = '-1'; 
$length = strlen($toEncode); 
$encoded = ''; 
$currentNbrChar = 0; 
for($i = 0; $i < $length; $i++){ 
    if($toEncode[$i] != $currentChar){ 
    if($currentChar != '-1'){ 
     $encoded .= chr(97 + $currentChar).$currentNbrChar; 
    } 
    $currentNbrChar = 0; 
    $currentChar = $toEncode[$i]; 
    } 
    $currentNbrChar ++; 
} 
if($currentChar != '-1'){ 
    $encoded .= chr(97 + $currentChar).$currentNbrChar; 
} 
echo $encoded; 
+0

谢谢!这工作完美。 – Alex 2010-04-02 13:25:37

2

这里有一个较短的版本:

function smush(str) { 
    return str.replace(/((.)\2*)/g, function(_, w, x) { 
    return x + w.length; 
    }); 
} 

编辑哦,我看你想用PHP编码;对不起,我不知道。下面是一个类似的精神解码器:

function unsmush(str) { 
    return str.replace(/(.)(\d+)/g, function(_, x, n) { 
    return new Array(parseInt(n, 10) + 1).join(x); 
    }); 
} 
0

仅供参考,你很可能gzip压缩您的数据和浏览将自动解压缩。对于大多数实现来说,这将比RLE更好地工作。显然不那么有趣。

0
$str="0000000001110002220033333"; 

//$c will count the number of occurances. 

$c=1; 

$lastInt=substr($str,0,1); 

$str=substr($str,1); 

$resultStr=''; 

$loopEnd=strlen($str); 


for($i=1; $i<=$loopEnd+1;$i++) 

{ 

    $nowInt=substr($str,0,1); 
    if($lastInt==$nowInt) 
    { 
     $c++; 
     $str=substr($str,1); 
    } 
    else 
    { 
     $char=chr((int)$lastInt + 97); 
     $resultStr=$resultStr.$char.$c; 
     $str=substr($str,1); 
     $c=1; 
     $lastInt=$nowInt; 
    } 
} 

// we use if condition since for loop will not take the last integer if it repeats. 

if($c>1) 
{ 

$char=chr((int)$lastInt + 97); 

$resultStr=$resultStr.$char.$c; 

} 

echo $resultStr; 
0
function compress($str) { 
$strArr = str_split($str.'0'); 
$count = 0; 
$resStr = ''; 
$strCheck = $strArr[0]; 
foreach($strArr as $key => $value) 
{ 
    if($strCheck == $value) 
    { 
     $count++; 
    } 
    else 
    { 
     if($count == 1) 
     { 
      $strCheck = $value; 
      $resStr .= $strArr[$key-1]; 
      $count=1; 
     } 
     elseif($count == 2) 
     { 
      $strCheck = $value; 
      $resStr .= $strArr[$key-1].$strArr[$key-1]; 
      $count=1; 
     } 
     else 
     { 
      $strCheck = $value; 
      $resStr .= $strArr[$key-1].$count; 
      $count=1; 
     } 
    } 

} 
return $resStr; 

}