【发布时间】:2016-09-15 11:40:55
【问题描述】:
我写这篇文章是为了避免 O(n!) 时间复杂度,但我现在只有伪代码,因为有些事情我不确定要实现。
这是我要传递给此脚本的文件格式。数据按第三列排序——起始位置。
93 Blue19 1 82
87 Green9 1 7912
76 Blue7 2 20690
65 Red4 2 170
...
...
256 Orange50 17515 66740
166 Teal68 72503 123150
228 Green89 72510 114530
代码说明:
我想创建一个数组来查找两条信息何时具有重叠长度。
输入文件的第 3 列和第 4 列是单个轨道线上的开始和停止位置。如果任何 row(x) 在第 3 列中的位置短于任何 row(y) 中第 4 列中的位置,则这意味着 x 在 y 结束之前开始并且存在一些重叠。
我想找到与 asnyrow 重叠的每一行,而不必将每一行与每一行进行比较。因为它们是排序的,所以我只需将一个字符串添加到表示一行的数组的内部数组中。 如果正在查看的新行不与数组中已有的行之一重叠,则(因为数组按第三列排序)没有其他行能够与数组中的行重叠并且可以将其删除.
这就是我的想法
#!/usr/bin/perl -w
use strict;
my @array
while (<>) {
my thisLoop = ($id, $name, $begin, $end) = split;
my @innerArray = split; # make an inner array with the current line, to
# have strings that will be printed after it
push @array(@innerArray)
for ( @array ) { # loop through the outer array being made to see if there
# are overlaps with the current item
if ( $begin > $innerArray[3]) # if there are no overlaps then print
# this inner array and remove it
# (because it is sorted and everything
# else cannot overlap because it is
# larger)
# print @array[4-]
# remove this item from the array
else
# add to array this string
"$id overlap with innerArray[0] \t innerArray[0]: $innerArray[2], $innerArray[3] "\t" $id : $begin, $end
# otherwise because there is overlap add a statement to the inner
# array explaining the overlap
代码应该产生类似
87 overlap with 93 93: 1 82 87: 1 7982
76 overlap with 93 93: 1 82 76: 1 20690
65 overlap with 93 93: 1 82 65: 2 170
76 overlap with 87 87: 1 7912 76: 2 20690
65 overlap with 87 87: 1 7912 65: 2 170
65 overlap with 76 76: 2 20690 65: 2 170
256 overlap with 76 76: 2 20690 256: 17515 66740
228 overlap with 166 166: 72503 123150 228: 72510 114530
这很难解释,所以如果你有任何问题可以问我
【问题讨论】:
-
相关:Quickest way to determine range overlap in Perl,它还询问查找范围集的重叠。在这种情况下,我认为您可以将您的集合与自身进行比较。
-
"如果任何 row(x) 在第 3 列中的位置短于任何 row(y) 中第 4 列中的位置,则这意味着 x 在 y 结束之前开始有一些重叠” 你确定吗?如果行 x 从 10 开始并在 20 停止,而行 y 从 30 开始并在 40 停止,则 10 小于 40 但没有重叠。如果您的数据以某种方式排序,您可能是正确的,但您所说的通常不是正确的。
-
你说的“单轨线”是什么意思?
-
@ThisSuitIsBlackNot:很好的来源。谢谢
-
@B.Monster:你要处理多少条记录?你的文件有多大?
标签: arrays perl multidimensional-array