Burrows–Wheeler transform

poj 1147

http://poj.org/problem?id=1147

字符串压缩,给出原字符串所有的移位字符串,按字典序排序,给出最后一列,还原第一行

00011

00110

01100

10001

11000

还原:由最后一列得到第一列,将最后一列与第一列合并,排序得到第一列与第二列,循环得到所有的列

压缩:字符串的重复出现,the,排序后he开头的字符串都以t结尾,最后一列有连续的t


由于循环,第一列的第一个0对应最后一列的第一个0,第一行第2个数字对应最后一列第一个0之后的下一位数字


#include<iostream>
using namespace std;


int main(){
    int n;
    cin>>n;
    bool *last = (bool *)malloc(n);
    bool *first = (bool *)malloc(n);
    int *next = (int *)malloc(sizeof(int)*n);
    int i,oneNum;
    oneNum = 0;
    for(i=0;i<n;i++){
        cin>>last[i];
        if(last[i]) oneNum++;        
    }
    memset(first,0,n-oneNum);
    memset(first+n-oneNum,1,oneNum);
    int zeroPos,onePos;
    zeroPos = onePos = 0;
    
    for(i=0;i<n;i++){
        if(first[i]==true){
            while(last[onePos]!=true) onePos++;
            next[i] = onePos;
            onePos++;                       
        }        
        else{
            while(last[zeroPos]!=false) zeroPos++;
            next[i] = zeroPos;
            zeroPos++;     
        }
    }
    
    int order = 0;
    for(i=0;i<n-1;i++){
        cout<<first[order]<<" ";
        order = next[order];        
    }
    cout<<first[order]<<endl;
    system("pause");
    
    return 0;    
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值