php实现bwt 算法

<?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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值