如何更改大组字节?该程序必须是可逆的。 [英] How do I shuffle large groups of bytes? The procedure must be reversible.

查看:65
本文介绍了如何更改大组字节?该程序必须是可逆的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

T 他的问题纯粹是数学问题,因为它们是主要的组合和概率。可以在不知道编程的情况下解决问题,但通过编程解决问题要容易得多。



在以下40字节组中,有3对相同的字节:



248,195,38,169, 202 202 ,133,240,232,204,142,40,56,89,215,138,15,217,123,33,183,105, 239 239 ,229,192,226,191,23,158,12,94,222,253,6,113, 146 146 ,183,209。



一对字节 169 202 是一对不同的字节。

一对字节 202 202 是一对相同的字节。



字节必须被洗牌,以便不出现相同字节的对。结果字节必须仅由不同字节对组成!



结果字节示例(交换2 * n和2 * n + 1字节,n = 1..19):



248,38,195, 202 ,169,133, 202 ,232,240,142,204,86 ,40,215,89,15,138,123,217,183,33, 239 ,105,229, 239 ,226,192,23,191,12 ,158,222,94,6,253, 146 ,113,183, 146 ,209。



程序必须是可逆的。而不是40个字节(在示例的情况下),该过程应该应用于1500字节的组。在1500字节的组中,大多数对是不同的字节。还有一些具有相同字节的对,但不超过10,并且它们不知道它们在组中的位置。不存在多个连续的相同字节。在1500字节的组中,所有字节至少出现一次。如果算法必须使用其他数字来混洗1500字节的组,则每组的这些数字必须相同。该算法不必在每次调用中都成功。从8个连续不同的1500字节组中就可以成功地对任何两个字节进行随机播放。



来自任何文件类型的任何字节组都可以转换为组在问题中描述。一组这样的1500字节对压缩和加密很感兴趣。



问题是有重组的字节 - 在它们的地方消除了相同的对是,但出现在其他地方。较大的字节组会出现此问题。一个40字节的组将解决几乎所有众所周知的用于混洗数字的算法,但是一组1500字节还没有解决任何一个!



最好提供delphi 7代码或伪代码的解决方案。



以下程序足以很好地模拟1500字节的组:



  var  
b: array [ 1 .. 1500 ] byte ;

程序MakePseudorandomNumbersWithEqualPairs(sum: longint ; max:word);
var
i,lastb: longint ;
范围: word ;
开始
随机化; // 准备随机数生成器
lastb:= sum; // 总字节数
范围:= max; // 0..max-1范围内的字节
for i:= 1 lastb 执行 // 他们可以是同一个邻居!
b [i]:=随机(范围);
end ;



程序调用后:



 MakePseudorandomNumbersWithEqualPairs(1500,256); 





数组b包含模型的字节数给定的团体。



我尝试过的事情:



  const  
lastb = 1500 ;
var
b,c: array [ byte ;> 1 .. lastb];

程序PerfectShuffle; // 完美随机播放
var
i: integer ;

开始
如果(lastb mod 2)= 0 // 只有偶数个字节!
然后
开始
for i:= 1 to (lastb div 2) do
c [i * 2 - < span class =code-digit> 1 ]:= b [i];
for i:=(lastb div 2)+1 lastb 执行
c [i * 2 -lastb]: = b [i];

for i:= 1 to lastb do
b [i]:= c [i];
end ;





输入:b

输出:b

解决方案

我会这样尝试:对于每个找到的对(从索引开始 i )选择一个随机数 r 并将索引 i 的项目与(i + r)(带环绕),除非这个项本身等于成对成员。在这样的infortunate事件中尝试使用(i + r + 1)(i + r + 2) ... ,直到找到合适的项目。

根据可转换要求,记下发生的掉期并按相反的顺序应用它们。 / BLOCKQUOTE>

The problem is purely mathematic because they are the dominant combinatorics and probability. It is possible to solve the problem without knowing programming, but it is much easier to solve it by programming.

In the following 40 byte group, there are 3 pairs of the same bytes:

248, 195, 38, 169, 202, 202, 133, 240, 232, 204, 142, 40, 86, 89, 215, 138, 15, 217, 123, 33, 183, 105, 239, 239, 229, 192, 226, 191, 23, 158, 12, 94, 222, 253, 6, 113, 146, 146, 183, 209.

A pair of bytes 169 202 is a pair of different bytes.
A pair of bytes 202 202 is a pair of the same bytes.

Bytes must be shuffled so that the pairs of the same bytes do not appear. The resulting bytes must be composed only of pairs of different bytes!

Example of resultant bytes (swap 2*n and 2*n+1 byte, n=1..19) :

248, 38, 195, 202, 169, 133, 202, 232, 240, 142, 204, 86, 40, 215, 89, 15, 138, 123, 217, 183, 33, 239, 105, 229, 239, 226, 192, 23, 191, 12, 158, 222, 94, 6, 253, 146, 113, 183, 146, 209.

The procedure must be reversible. Instead of 40 bytes (in case of example), the procedure should be applied to a group of 1500 bytes. In a group of 1500 bytes, most pairs are different bytes. There are also pairs with the same bytes, but there are no more than 10 and they do not know where they are in the group. Multiple consecutive same bytes do not exist. In a group of 1500 bytes, all bytes appeared at least once. If the algorithm has to use additional numbers to shuffle a group of 1500 bytes, then these numbers must be the same for each group. The algorithm does not have to be successful in every call. It is enough that from 8 consecutive different groups of 1500 bytes it succeeds to shuffle any two.

Any group of bytes from any file type can be converted into a group as described in the problem. A group of such 1500 bytes is of interest for compression and encryption.

The problem is that there is recombination of bytes - the same pairs are eliminated at the places where they were, but appear elsewhere. This problem occurs with larger groups of bytes. A 40-byte group will solve almost every well-known algorithm for shuffling numbers, but a group of 1500 bytes has not solved any one!

It is desirable to provide a solution in delphi 7 code or pseudocode.

The following procedure sufficiently well model a group of 1500 bytes:

var
  b : array [1..1500] of byte;

procedure MakePseudorandomNumbersWithEqualPairs (sum : longint; max : word);
var
  i, lastb : longint;
  range : word;
begin
  Randomize;                    // Prepare a random number generator
  lastb := sum;                 // How many total bytes
  range := max;                 // Bytes from the range 0..max-1
  for i := 1 to lastb do        // They can be the same neighbors!      
    b[i] := Random (range);
end;


After the procedure call:

MakePseudorandomNumbersWithEqualPairs (1500, 256);



array b contains bytes that model the given group.

What I have tried:

const
  lastb = 1500;
var
  b, c : array [1..lastb] of byte;
  
procedure PerfectShuffle;      // Perfect shuffle
var
  i : integer;

begin
  if (lastb mod 2) = 0     // only if is a even number of bytes!
   then
    begin
      for i := 1 to (lastb div 2) do
        c[i*2-1] := b[i];
      for i := (lastb div 2)+1 to lastb do
        c[i*2-lastb] := b[i];

      for i := 1 to lastb do
        b[i] := c[i];
    end;



Input: b
Output: b

解决方案

I would try this way: for each found pair (starting at index i) choose a random number r and swap the item at index i with the item at (i+r) (with wrap around) unless such item is itself equal to the pair members. In such an infortunate event try with (i+r+1), (i+r+2) ..., until you find a suitable item.
As per reversibility requirement, take note of the occurred swaps and apply them in the reverse order.


这篇关于如何更改大组字节?该程序必须是可逆的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆