gpt4 book ai didi

arrays - 将重复/重复模式识别为来自父数组的子数组

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

我有一个典型的模式搜索问题,我需要确定多个模式在数组中出现的位置并将它们挑出来。

例如:['horse', 'camel', 'horse', 'camel', 'tiger', 'horse', 'camel', 'horse', 'camel']

函数应该返回

['horse', 'camel'], 
['horse', 'camel', 'horse'],
['camel', 'horse', 'camel'],
['horse', 'camel', 'horse', 'camel']

即查找在可以成为子数组的数组中重复的模式,

或者另一种定义方式是 -> 找到所有在主数组中出现超过 1 次的子数组。

即结果数组应该有 length > 1 ->

[1, 2, 3, 1, 2, 1, 4, 5] => [1,2,3][1, 4,5] 都是子数组,但是 [1,2,3] 是循环/重复子数组,而不是 [1,4,5]

寻找合适的高效算法而不是强力循环解决方案。

最佳答案

这可能不是您想要的,但我不知道您已经尝试过什么,所以它可能会有用。这是我的直接方法,可能属于您的“强力循环解决方案”,但我想试一试,因为没有人发布完整的答案。

在Java中:

// use this to not add duplicates to list
static boolean contains (List<String[]> patterns, String[] pattern){
for(String[] s: patterns)
if (Arrays.equals(pattern,s)) return true;
return false;
}


/**
*
* @param str String array containing all elements in your set
* @param start index of subarray
* @param end index of subarray
* @return if subarray is a recurring pattern
*/
static boolean search (String[] str,int start,int end) {
// length of pattern
int len = end - start + 1;

// how many times you want pattern to
// appear in text
int n = 1;

// increment m if pattern is matched
int m = 0;

// shift pattern down the array
for (int i = end+1; i <= str.length - len; i++) {
int j;
for (j = 0; j < len; j++) {
if (!str[i + j].equals(str[start + j]))
break;
}

// if pattern is matched at [i to i+len]
if (j == len) {
m++;
if (m == n) return true;
}
}
return false;
}


/**
*
* @param str String array containing all elements in your set
* @return a list of subsets of input set which are a recurring pattern
*/
static List<String[]> g (String[] str) {
// put patterns in here
List<String[]> patterns = new ArrayList<>();

// iterate through all possible subarrays in str
for(int i = 0; i < str.length-1; i++){
for(int j = i + 1; j < str.length; j++){

// if a pattern is found
if (search(str,i,j)) {
int len = j-i+1;
String[] subarray = new String[len];
System.arraycopy(str,i,subarray,0,len);
if (!contains(patterns,subarray))
patterns.add(subarray);

}
}
}
return patterns;
}

public static void main(String[] args) {

String[] str = {"horse", "camel", "horse", "camel", "tiger",
"horse", "camel", "horse", "camel"};
// print out
List<String[]> patterns = g(str);
for (String[] s: patterns)
System.out.println(Arrays.toString(s));
}

输出:

[horse, camel]
[horse, camel, horse]
[horse, camel, horse, camel]
[camel, horse]
[camel, horse, camel]

正如我发表的评论中提到的:

[camel, horse] 会包含在输出中吗?”

我的输出与此相关,因为在索引 [1-2][6-7 处有 2 个 [camel, horse] 实例]。但也许我完全误解了你的问题并且我不理解这些限制。

至于优化,例如 search(...) 方法只是一个简单的子字符串搜索,还有一些更优化的方法可以做到这一点,例如Knuth–Morris–Pratt .抱歉,如果这正是您不想要的,但也许有一些用处

关于arrays - 将重复/重复模式识别为来自父数组的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40125318/

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