一个数组算法问题
如果我有两个数组。例如, 一个数组是 int[] one={1,2,4,6,54,3,34};
另一个是 int[] Two={12,1,2,4 ,7,8,54,3,34,5};
问题是我怎样才能在一两个之间获得“相同的部分”。 示例中的“相同部分”是[1,2,4]和[54,3,34]。
PS你可以使用伪语言 ,c,c#,java,php或其他语言。
PS现在我也说清楚了 零件。相同的零件元素有 清单。
PSI 更改了示例,数组中每个项目的值不相等 (你可以看我的例子。)
- 至少有两个项目
- 与两个数组中匹配项目的索引匹配,不需要匹配 ,但相同部分必须是连续的。
If I have two arrays.For example,
One array is int[] one={1,2,4,6,54,3,34};
the other is int[] two={12,1,2,4,7,8,54,3,34,5};
The problem is how can I get "the same parts" between one and two.
"The same parts" in the example are [1,2,4] and [54,3,34].
P.S.You can use pseudo language
,c,c#,java,php or other language.
P.S. Now I make clear the same
parts.the same parts elements have
the lists.
P.S.I have change the example,and value of each item in the array isn't equal
(You can see my example.)
- at least two items match
- the index of the match item in the two arrays not necessary to match
,but the same parts must be continuous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为两个数组(视为“字符串”)构建一个 后缀树 并比较两个数组树。
特别是,您可以选择两棵树中的一棵(例如与较小数组关联的一棵)(称为 A)并开始遍历它,模仿另一棵树上的移动(称为 B)。
如果您位于树 A 的节点 u 中,并且无法从该节点复制任何“移动”到树 B 的相应节点,那么您就找到了“最大匹配”(从 root 拼写到 u 的匹配)你可以修剪以u为根的树A的子树。
这只是一个想法,你必须以此为基础;请注意,您可以在 O(n) 中构建后缀树,并且这种“双相似性”也是 O(n),所以它看起来是最优的。
You can build a suffix tree for the two arrays (seen as 'strings') and compares the two trees.
In particular, you can choose one of the two trees (the one associated with the smaller array, say) (call it A) and start traversing it, mimicking the moves on the other tree (call it B).
If you are in a node u of tree A and you can't replicate any "move" from this node to the corresponding one of tree B, then you've found a "maximal match" (the one spelled from root to u) and you can prune the subtree of tree A rooted on u.
This is just an idea, you must build up on it; note that you can build a suffix tree in O(n) and this kind of "bisimilarity" is O(n) too, so it looks like optimal.
这可能是最长公共子序列问题。
This is probably the longest common subsequence problem.
经过一些优化,几乎是蛮力。最坏情况 O(n^4)。 n 是较短数组的大小。
Nearly a brute force with some optimizations. Worst case O(n^4). n is size of shorter array.