我有个主意。
首先,我们应该使 List 排序。就像 hmjd 描述的那样。
在比较两个字符串时,我们可以记录一些信息。
例如,
表二维数组差异记录两个字符串不同的索引。
string[2] = {"abc","abd"}
list[5] = {"aab","abb","abc","bcd","ef"}
dif[0][0] = 1 ("abc" and "aab" differ at index 1)
dif[0][1] = 2 ("abc" and "abb" differ at index 2)
dif[0][2] = -1 ("abc" and "abc" are same, so we use -1 to represent two strings are same)
dif[0][3] = 0 ("abc" and "bcd" differ at index 0)
dif[0][4] = 0 ("abc" and "eg" differ at index 0)
当我们需要将一个新字符串与列表中的字符串进行比较时。我们首先在已比较的字符串中找到最相似的字符串。例如,“abd”是需要判断的字符串。我们找到“abc”。"abd" 和 "abc" 在索引 2 处不同。因此,当我们比较 "adb" 和 list 中的字符串时,我们不需要比较在索引 2 之前的索引中不同 "abc" 的字符串。例如,我们不需要比较“abd”和“ef”,因为“abd”在索引 2 处与“abc”不同,而“abc”在索引 0 处与“ef”不同。
我的想法很粗略,有很多细节需要考虑。我认为它很有用,尤其是在大规模问题中。