A difficult question about algorithms, writing inverse Algorithms

Source: Internet
Author: User

A difficult question about algorithms, writing inverse Algorithms

Reprint Source: http://blog.liutaotao.com/blogview.asp? Logid = 38

Recently I have been studying the compression algorithm. I just learned a compression algorithm and transformed it into a test question.
This question is very difficult, and this algorithm is very clever. If I do not know the answer, I am not sure I can do it.
If you are confident about the algorithm, try it. If you can't do it, and want to know the answer, contact me.
The complete decode code is provided below. The encode is empty and the code is filled in to make the algorithm work.

This algorithm can be widely used in registration machines and network transmission.

# Include <windows. h>
# Include <stdio. h>

Class cbitencoder
{
Pbyte m_pbuf;
Uint m_len;

Uint prob;
Uint _ cachesize;
Byte _ cache;

Uint64 low;
Uint uisom;

Void writebyte (byte B) {m_pbuf [m_len ++] = B ;}
Void Init ()
{
Prob = (1 <10 );
Low = 0;
Uisom = 0 xffffffff;
_ Cachesize = 0;
_ Cache = 0;
}
Void shiftlow ()
{
13 lines of code are empty;
}
Public:
Void setoutputbufptr (pbyte BUF)
{
Init ();
M_pbuf = Buf;
M_len = 0;
}
Int getlength () {return m_len ;}

Void flushdata ()
{
2 lines of code are empty;
}

Void bencode (uint symbol)
{
17 lines of code are empty;
}
};
// --------------- Encoder or Decoder

Class cbitdecoder
{
Pbyte m_psrc;
Int m_srclen;

Uint uisom;
Uint uiany;
Uint prob;

Byte readbyte ()
{
M_srclen --;
Return * m_psrc ++;
}
Void Init ()
{
Prob = (1 <10 );
Uiany = 0;
Uisom = 0 xffffffff;
For (INT I = 0; I <4; I ++)
Uiany = (uiany <8) | this-> readbyte ();
}
Public:
Void setsrcbuf (pbyte psrc, int srclen)
{
M_psrc = psrc;
M_srclen = srclen;
Init ();
}
Int getremainbyte () {return m_srclen ;}

Bool bdecode ()
{
Bool B;
Uint u = (this-> uisom> 11) * This-> prob;
If (this-> uiany <u)
{
This-> uisom = u;
This-> prob + = (1 <11)-This-> prob)> 5;
B = false;
}
Else
{
This-> uisom-= u;
This-> uiany-= u;
This-> prob-= (this-> prob)> 5;
B = true;
}
If (this-> uisom <(1 <24 ))
{
This-> uiany = (this-> uiany <8) | this-> readbyte ();
This-> uisom <= 8;
}
Return B;
}
};

Int test_encode (pbyte psrc, int srclen, pbyte BUF)
{
Cbitencoder bitencoder;
Bitencoder. setoutputbufptr (BUF );

For (INT I = 0; I <srclen; I ++)
{
Byte B = psrc [I];
For (Int J = 0; j <8; j ++)
{
Bitencoder. bencode (B> J) & 1 );
}
}
Bitencoder. flushdata ();

Return bitencoder. getlength ();
}

Int test_decode (pbyte psrc, int srclen, pbyte BUF)
{
Cbitdecoder bitdecoder;
Bitdecoder. setsrcbuf (psrc, srclen );

Int totallen = 0;
For (;;)
{
Byte B = 0;
For (INT I = 0; I <8; I ++)
{
B + = (bitdecoder. bdecode () <I );
If (bitdecoder. getremainbyte () <0)
Return totallen;
}
* Buf ++ = B;
Totallen ++;
}
Return totallen;
}

Void main ()
{
Char * psrc = "exam by liutaotao 20070728 ";
Int srclen = strlen (psrc );
Byte Buf [3000];
Int Len = test_encode (pbyte) psrc, srclen, Buf );
Printf ("srclen = % d encode Len = % d/N", srclen, Len );

Byte outbuf [3000];
Int len2 = test_decode (BUF, Len, outbuf );
Printf ("decode Len from % d to % d/N", Len, len2 );
If (len2> = srclen &&! Memcmp (psrc, outbuf, srclen ))
Printf ("OK/N ");
Else
Printf ("error/N ");
}
// This compression algorithm features simple code and high speed
// Another disadvantage is that it cannot effectively determine whether the decoding has ended. Sometimes, several more bytes are returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.