以下是在Python中构建后缀数组的一种非常简单的方法:
def sort_offsets(a, b):
return cmp(content[a:], content[b:])
content = "foobar baz foo"
suffix_array.sort(cmp=sort_offsets)
print suffix_array
[6, 10, 4, 8, 3, 7, 11, 0, 13, 2, 12, 1, 5, 9]
然而,“content[a:]”会复制content,当content变得很大时,这将变得非常低效。因此,我想知道是否有一种方法可以在不复制它们的情况下比较这两个子字符串。我尝试过使用内置的缓冲区(buffer-builtin),但它没有起作用。