算法解决重复模式问题的想法

时间:2017-09-21 09:13:26

标签: algorithm

有一个包含数字的列表,如果它具有至少一个 重复模式,则重复,否则它不重复。

重复模式是一系列相邻的模式 数字大于 T 且长度 N ,并且在列表中重复显示 M 次,并且重复模式的每个实例都不重叠彼此。

例如:

list A: [6, 2, 4, 2, 4, 5, 3, 2, 4, 6, 2, 4, 2, 3, 6, 2, 4]
list B: [1, 2, 4, 2, 2, 2, 2, 2, 4, 1, 2, 4, 2, 3, 1, 2, 4]
T: 1
N: 3
M: 3
  • 列表A是重复的,因为它具有重复模式[6,2,4]
  • 列表B不重复,即使[1,2,4],[2,2,2]重复出现3次,但1不大于1,并且三个重复实例之间存在重叠部分[2,2,2],所以它们都不是重复模式。

问题:这个问题可以转化为解决方案的已知问题吗?或者有任何可行的解决方案吗?

P.S。这里不考虑性能问题。

1 个答案:

答案 0 :(得分:0)

我觉得这个想法应该适用于O(L * N):

function isRepetitive(list : List<Integer>[1..L], T : Integer,
                      N : Integer, M : Integer) -> Boolean
    // Map from pattern to (count, last index)
    patternCounts : Map<List<Integer>[1..N], Tuple<Integer, Integer>> = empty map
    for i = 1 .. (L - N + 1)
        pattern = list[i:i + N]
        if any(j <= T for j in pattern)
            continue
        end
        if pattern not in patternIndices
            patternIndices[pattern] = (1, i)
        else
            (lastCount, lastIndex) = patternIndices[pattern]
            if i >= lastIndex + N
                patternIndices[pattern] = (lastCount + 1, i)
            end
        end
        if patternIndices[pattern][0] >= M
            return true
        end
    loop
    return false
end

当您在列表中找到低于阈值的元素时,您甚至可以对快进i进行轻微优化。