Burrows–Wheeler transform

来源:互联网 发布:免费注册淘宝会员 编辑:程序博客网 时间:2024/04/26 13:08

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
原创粉丝点击