gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:20:27 26 4
gpt4 key购买 nike

后缀树或后缀数组能否有效地用于数字?

例如:

可以和数组一起使用吗 [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 开始的重复派生的

最佳答案

这里是

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,则将其添加到集合中。

希望对你有帮助

关于java - 带数字的后缀数组/后缀树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com