0

后缀树或后缀数组可以有效地与数字一起使用吗?

例如:

它可以与数组[1,2,3,4,5,3,9,8,5,3,9,8,6,4,5,3,9,11,9,8,7,11]一起使用以从数组的内容中提取所有可能的不重叠重复的所有大小的子字符串吗?如果是这样,您能否提供相同的实现。我试图达到同样的效果,但还没有找到有效的解决方案。

预期成绩:

4,5
4,5,3
4,5,3,9
5,3
5,3,9
5,3,9,8
...

考虑数组 : [1,2,3,4,5,9,3,4,5,9,3,3,4,5,9,3],非重叠重复序列意味着提取的组:3,4,5,9,3源自从索引 2 到 6 和 11 到 15 和 NOT 6 到 10 开始的重复

4

1 回答 1

1

这里是

public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 3, 9, 8, 5, 3, 9, 8, 6, 4, 5, 3, 9, 11, 9, 8, 7, 11}; // expect : 2,3  /  2,3,4  /  3,4
    Set<String> strings = new HashSet<>();
    // for every position in the array:
    for (int startPos = 0; startPos < arr.length; startPos++) {

        // from the actual position + 1 to the end of the array
        for (int startComp = startPos + 1; startComp < arr.length; startComp++) {
            int len = 0; // length of the sequence
            String sum = "";
            // while not at the end of the array, we compare two by two
            while (startComp + len < arr.length && arr[startPos + len] == arr[startComp + len]) {
                sum += arr[startPos + len];
                // if detected sequence long enough
                if (len > 0) {
                    strings.add(sum);
                }
                len++;
            }
            // just to gain some loop
            startComp = startComp + len;
        }
    }
}

对于您的数据,我的结果是:

98 453 4539 45 5398 539 398 53 39

基本上,循环遍历您的数组。Foreach 字母与其右侧的每个字母进行比较。如果找到相同的字母,则比较增长的序列,如果其长度>1,则将其添加到集合中。

希望能帮助到你

于 2016-01-21T16:26:44.047 回答