带数字的后缀数组/后缀树

时间:2016-01-21 14:22:35

标签: java algorithm suffix-tree suffix-array longest-substring

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

例如:

是否可以与数组[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以及非6到10的重复

1 个答案:

答案 0 :(得分: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,则将其添加到集合中。

希望有所帮助