根据有序字符串对生成直观的唯一字符串?

时间:2017-06-12 16:25:32

标签: string algorithm

相关:Generate a unique string based on a pair of strings

我想生成直观唯一字符串来表示有序字符串对。

显然,stringA + stringB非常直观,但如果您考虑"st" + "ring" == "stri" + "ng" == "string",则不是唯一的。

此外,与链接的OP不同,我希望uniqueString(stringA, stringB) != uniqueString(stringB, stringA),即非交换。 像MD5(stringA) - MD5(stringB)这样的东西可能会考虑链接的OP,但我觉得它非常不直观。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

如果遇到这样的问题,我会尝试类似CSV的方法,例如

  • stringA + stringB => stringA;stringB

  • stringA + string;B => stringA;"string;B"

  • stringA + string"B => stringA;"string""B"

答案 1 :(得分:1)

Encode the length of the first string into the resulting string; that way, you know where the split is, and "xy" + "z" is different from "x" + "yz".
Zero-pad the length, so that it always has the same number of digits (depending on the maximum length of the strings).

Examples (with a maximum string length of 999):

"xxx" + "yyy" = "003xxxyyy"  
"xx" + "xyyy" = "002xxxyyy"
"xxxyyy" + "" = "006xxxyyy"  
"" + "xxxyyy" = "000xxxyyy"  
"" + ""       = "000"

Alternatively, if the maximum length of the string is unknown, you could use a delimiter after the length:

"xxx" + "yyy" = "3;xxxyyy"  

You don't have to use a special character for this, or escape the delimiter in the strings, because there is no ambiguity:

"a;b" + ";c;" = "3;a;b;c;" = length + delimiter + "a;b;c;"

答案 2 :(得分:0)

这感觉非常像序列化问题......将两个值放在同一个地方,然后仍然可以将它们分开。

最简单的方法之一是使用分隔符àlacsvs,但这需要您实现一个唯一的字符或字符序列。

删除此问题非常简单,只需在字符串中所有分隔符实例之前添加'\'以及'\'的所有实例。

举个例子:

"hello, " + "wor\d"
"hello\, " + "wor\\d" //Add in the escape characters
"hello\, ,wor\\d" //Second comma is not escaped, parser knows to split the string back into two components there