算法导论-C++实现-UDC uniquely decodable唯一可编码问题算法

本文深入探讨了独特编码与解码算法的核心原理及应用,通过实例演示算法的实现过程,强调其在信息处理领域的关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string.h>
#include <malloc.h>
#define CNUM 2
#define CWNUM 5
#define MAXCWLENGTH 10
using namespace std;
char* character;
char** codeword;
struct SContainer *ss;
struct SContainer *ssTail;
struct SContainer{
    char* s;
    struct SContainer *next;
};
bool isInSContainer(char* str,struct SContainer *t){
    struct SContainer *temp;
    temp=t;
    while(temp!=NULL){
//        cout<<str<<" and "<<temp->s<<" compare"<<endl;
        if(!strcmp(temp->s,str)){
            return true;
        }
//        cout<<str<<" and "<<temp->s<<" not equal"<<endl;
        temp=temp->next;
    }
    return false;
}
void initialize(){
    character=new char[CNUM];
    character[0]='0';
    character[1]='1';
    codeword=new char*[CWNUM];
    codeword[0]="01";
    codeword[1]="001";
//    codeword[2]="01";
    codeword[2]="10";
    codeword[3]="010";
    codeword[4]="100";
//    codeword[4]="1000";
    ss=NULL;
    ssTail=NULL;
    /*
    if((ss=(SContainer *)malloc(sizeof(SContainer)))==NULL){
        cout<<"not enough space."<<endl;
        exit(1);
    }
    */
  //  cout<<character[1]<<endl;
//    cout<<codeword[2]<<endl;
}
bool firstStep(){
    for(int i=0;i<CWNUM-1;i++){
        for(int j=i+1;j<CWNUM;j++){
            if(!strcmp(codeword[i],codeword[j])){
                return false;
            }
        }
    }
    return true;
}
char* getSuffixOf(char* Ci,char *Cj){
    int index=0;
    char* suffix=new char[MAXCWLENGTH+1];
    while(Ci[index]!='/0' && Cj[index]!='/0'){
        if(Ci[index]!=Cj[index])
            return NULL;
        index++;
    }
//    cout<<"Finish testing"<<endl<<"Begin to get suffix"<<endl;
//    cout<<"index="<<index<<endl;
    int i=0;
    while(Ci[index]!='/0'){
        suffix[i]=Ci[index];
        index++;
        i++;
    }
    while(Cj[index]!='/0'){
        suffix[i]=Cj[index];
        index++;
        i++;
    }
    suffix[i]='/0';
//    cout<<"Get suffix :"<<suffix<<endl;
    return suffix;
}
void addInSContainer(char *t){
    struct SContainer *temp;
    if((temp=(SContainer *)malloc(sizeof(SContainer)))==NULL){
        cout<<"not enough space."<<endl;
        exit(1);
    }
    temp->s=t;
    temp->next=ss;
    ss=temp;
}
void addInSContainerTail(char *t){
    struct SContainer *temp;
    if((temp=(SContainer *)malloc(sizeof(SContainer)))==NULL){
        cout<<"not enough space."<<endl;
        exit(1);
    }
    temp->s=t;
    temp->next=NULL;
    if(ss==NULL){
        ssTail=temp;
        ss=temp;
    }
    else{
        ssTail->next=temp;
        ssTail=temp;
    }
}
void secondStep(){
    for(int i=0;i<CWNUM-1;i++){
        for(int j=i+1;j<CWNUM;j++){
            char* temp;
            temp=getSuffixOf(codeword[i],codeword[j]);
            if(temp!=NULL && !isInSContainer(temp,ss)){
                addInSContainerTail(temp);
            }
        }
    }
}
bool finalStep(){
    struct SContainer *ttt;
    ttt=ss;
    char *temp;
    while(ttt!=NULL){
        temp=ttt->s;
        for(int i=0;i<CWNUM;i++){
            if(!strcmp(codeword[i],temp)){
                return false;
            }
            char* suf;
            suf=getSuffixOf(temp,codeword[i]);
            if(suf!=NULL && !isInSContainer(suf,ss)){
                addInSContainerTail(suf);
            }
        }
        ttt=ttt->next;
    }
    return true;
}
int main(){
    initialize();
    /*
    ss->s="01";
    struct SContainer *s1;
    if((s1=(SContainer *)malloc(sizeof(SContainer)))==NULL){
        cout<<"not enough space."<<endl;
        exit(1);
    }
    ss->next=s1;
    s1->s="10";
    s1->next=NULL;
    if(isInSContainer("10",ss)){
        cout<<"exist"<<endl;
    }
    else{
        cout<<"not exist"<<endl;
    }
    */


    if(firstStep()){
        cout<<"No error occurs during first step"<<endl<<"There goes the second step"<<endl;;
        secondStep();
        cout<<"Second step finished"<<endl<<"There goes the final step"<<endl;
        if(finalStep())
            cout<<"Final step finished"<<endl<<"It is uniquely decodable"<<endl;
        else
            cout<<"Final step finished"<<endl<<"It is not uniquely decodable"<<endl;
    }
    else
        cout<<"Not uniquely decodable"<<endl;

 


//    if(getSuffixOf("0101001","011")==NULL)
//        cout<<"No suffix"<<endl;
//    addInSContainerTail("00");
//    addInSContainerTail("01");
//    addInSContainerTail("10");
//    addInSContainerTail("11");
//    if(isInSContainer("111",ss)){
//        cout<<"exist"<<endl;
//    }
//    else{
//        cout<<"not exist"<<endl;
//    }
//    struct SContainer *ttt;
//    ttt=ss;
//    while(ttt!=NULL){
//        cout<<ttt->s<<endl;
//        ttt=ttt->next;
//    }
//    cout<<"Tail:"<<ssTail->s<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值