有没有一种方法可以将一个字符串随机分成不同的字符串数组,并返回相同的字符串 [英] is there a way randomly separate a string into different string array and get back same same string
问题描述
例如,我有
String Text = "ABCDEFGHIJKLMNOPQ";
通过一些代码进入
String[] text1 = "AOCN";
String[] text2 = "JQBF";
String[] text3 = "DMG";
String[] text4 = "HPI";
String[] text5 = "KEL";
然后输入一些代码将其恢复为
then some code to get it back to
字符串文本= ABCDEFGHIJKLMNOPQ;
String Text = "ABCDEFGHIJKLMNOPQ";
这可能吗?
我要实现的目标是将溢出的字符随机存储到5个不同的字符串数组中,并使用代码将其恢复为原始文本
is this possible? what I am trying to achieve is random storing the spilt character into 5 different String Array and use code to revert it back to it original text
推荐答案
假设对数组进行字母随机分配的请求是因此是可逆的伪随机(或表面上任意)分配,那么这样做的一种技术是本质上使用换位密码。
Assuming that the request for a "random" allocation of letters to arrays is for a pseudo-random (or, perhaps, a superficially arbitrary) allocation that is therefore reversible, one technique to do this would be to essentially use a transposition cipher.
然后,该算法将类似于:
The algorithm would then be something like:
- 运行转置密码输入的文本。
- 将转置后的文本拆分为数组。
原始文本将然后通过反转两个步骤即可获得。
The original text would then be obtained by reversing the two steps.
(编辑)
换位密码密钥可以由来自<$的伪随机数流组成c $ c> 1 到 n
,其中 n
是其中输入字符串将被分割。因此,扩展算法如下:
The transposition cipher key could consist of a stream of pseudo-random numbers from 1
to n
where n
is the number of strings into which the input string is to be split. Thus, an expanded algorithm would read as follows:
- 生成伪随机数列表,
p 长度为
m
的code>,其中m
是输入字符串的长度。 - 对于所有
i
,在输入字符串中分配第i
个字母到输出字符串号p [i]
。
- Generate a list of pseudo-random numbers,
p
, of lengthm
, wherem
is the length of the input string. - For all
i
, assign thei
th letter in the input string to the output string numberp[i]
.
重新组合原始字符串:
- 对于所有
i
,获取i
字符串号p [i]
中下一个未使用的字母中的第一个字母。
- For all
i
, obtain thei
th letter in the string from the next unused letter in string numberp[i]
.
这篇关于有没有一种方法可以将一个字符串随机分成不同的字符串数组,并返回相同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!