[英]Suffix array/suffix tree with numbers
后綴樹或后綴數組可以有效地與數字一起使用嗎?
例如:
可以與數組[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的重復開始的
這里是
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,則將其添加到集合中。
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.