8

次のような非常に長い整数シーケンスがあります (任意の長さ!):

0000000001110002220033333

今、この文字列を次のような圧縮されたものに変換するアルゴリズムが必要です

a9b3a3c3a2d5

これは、「a 9 回、次に b 3 回、次に a 3 回」という意味で、「a」は 0、「b」は 1、「c」は 2、「d」は 3 を表します。

どうやってそれをしますか?これまでのところ、適切なものは何も頭に浮かびませんでした。何を検索すればよいか本当にわからなかったので、Google ではうまくいきませんでした。この種のエンコード/圧縮は何と呼ばれますか?

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');     
};
4

6 に答える 6

7

ランレングスエンコーディングと呼ばれる

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;
}
于 2010-04-02T13:09:41.183 に答える
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;
于 2010-04-02T13:18:28.260 に答える
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);
  });
}
于 2010-04-02T13:25:11.407 に答える
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;
于 2010-04-02T13:46:01.877 に答える
0

参考までに、データを gzip で圧縮すると、ブラウズによって自動的に解凍されます。ほとんどの実装では、これは RLE よりもうまく機能します。しかし、明らかに楽しくありません。

于 2010-04-02T13:41:05.507 に答える
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;

}

于 2015-07-16T06:26:27.113 に答える