简体   繁体   English

将字符串数组编码为单个字符串

[英]Encoding an array of strings into a single string

You're given an array of strings where each character in the string is lowercase. 您将得到一个字符串数组,其中字符串中的每个字符都小写。 Each character and the length of each string is randomly generated. 每个字符和每个字符串的长度都是随机生成的。 Encode the string such that: 1. The encoded output is a single string with minimum possible length 2. You should be able to decode the string later 对该字符串进行编码,以便:1.编码后的输出是单个字符串,且长度尽可能短2.您应该可以在以后对该字符串进行解码

I am thinking the mention of each character being lowercase is key here. 我认为这里提到每个字符都是小写是关键。 Since there are only 26 lowercase characters, maybe we can encode them using 5 bits instead of 8 bits and then pack them. 由于只有26个小写字符,也许我们可以使用5位而不是8位对它们进行编码,然后打包它们。 But I am not sure how to implement this bit packing while looping over the array of strings 但是我不确定在循环字符串数组时如何实现这种位打包

For 26 characters and a separator you could use base32. 对于26个字符和一个分隔符,您可以使用base32。 Basically concatenate the strings with a delimiter and then do a base32 decode - should be easy to find code for that. 基本上用定界符将字符串连接起来,然后进行base32 解码 -应该很容易找到用于此的代码。 Just do not use those characters that result in 4-5 zeros in binary so that you do not accidentally have the null terminator in the middle of your string. 只是不要使用那些会在二进制中导致4-5个零的字符,这样您就不会在字符串中间意外地使用空终止符。

For decoding you'll do base32 encode and then split the string at delimiters. 对于解码,您将进行base32 编码 ,然后在定界符处分割字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM