strcmp for python or how to sort substrings efficiently (without copy) when building a suffix array

后端 未结 4 1800
孤独总比滥情好
孤独总比滥情好 2021-02-06 00:24

Here\'s a very simple way to build an suffix array from a string in python:

def sort_offsets(a, b):
    return cmp(content[a:], content[b:])

content = \"foobar          


        
4条回答
  •  半阙折子戏
    2021-02-06 01:05

    +1 for a very interesting problem! I can't see any obvious way to do this directly, but I was able to get a significant speedup (an order of magnitude for 100000 character strings) by using the following comparison function in place of yours:

    def compare_offsets2(a, b):
        return (cmp(content[a:a+10], content[b:b+10]) or
                cmp(content[a:], content[b:]))
    

    In other words, start by comparing the first 10 characters of each suffix; only if the result of that comparison is 0, indicating that you've got a match for the first 10 characters, do you go on to compare the entire suffices.

    Obviously 10 could be anything: experiment to find the best value.

    This comparison function is also a nice example of something that isn't easily replaced with a key function.

提交回复
热议问题