<?php
// super Jia's implementation of the BWT
//2015年5月11日15:50:54
$string=$_POST['string1'];
$bwt_string=bwt($string);
print"oringinal string:$string";
echo"</br>";
echo"</br>";
print"after revert:$bwt_string";
echo"</br>";
function suffixArray($string){
$len=strlen($string);
$suffix="";
$sa=array();
for($i=$len;$i>=0;$i--){
$suffix=substr($string,$i)."\$";
$sa[$i]=$suffix;
asort($sa);
}
return $sa;
}
function bwt($string){
$sa=suffixArray($string);
$bwt_string="";
foreach($sa as $key=>$value){
if($key==0){
$bwt_string.= "\$";
}
else{
$bwt_string.=$string[$key-1];
}
}
return $bwt_string;
}
//them click the button to convert back
?>
</pre><pre code_snippet_id="666088" snippet_file_name="blog_20150513_3_7081523" name="code" class="php">需要求改一下,原始的string我是从html 获取的,但是并没有附上html脚本
产生了BWT string
找回原string:
<pre name="code" class="php">function rankBwt($bwt_string){
$ranks=array();
$tots=array();
for($i=0;$i<strlen($bwt_string);$i++){
$c=$bwt_string[$i];
#print("$c ");
if(!array_key_exists($c,$tots)){
#print("no! ");
$tots[$c]=0;
}
#print("$tots[$c] ");
array_push($ranks,$tots[$c]);
$tots[$c]+=1;
}
#print(" $bwt_string");
#print_r($tots);
$re=array();
$re[0]=$ranks;
$re[1]=$tots;
return $re;
}
function firstCol($tots){
$first=array();
$totc=0;
ksort($tots);
foreach($tots as $key=>$value){
$first[$key]=array($totc,$totc+$value);
$totc+=$value;
}
#print_r($first['$']);
return $first;
}
#rankBwt($bwt_string);
#print_r($ranks);
function reverseBwt($bwt_string){
$re=rankBwt($bwt_string);
$ranks=$re[0];
$tots=$re[1];
$first=firstCol($tots);
$rowi=0;
$t="$";
while($bwt_string[$rowi]!="$"){
$c=$bwt_string[$rowi];
$t=$c.$t;
$rowi=$first[$c][0]+$ranks[$rowi];
}
print("$t");
}
reverseBwt($bwt_string);
?>
参考:http://www.cs.jhu.edu/~langmea/resources/bwt_fm.pdf