问题描述
|
我有类似的对象列表:
class PairingStrand
{
int startInt; // the start pos
int endInt; // the end pos
int Index; // the index of pairing
int Strand; // Strand is either \"5\" or \"3\"
}
每个位置(整数)只能与一个对象关联。
具有“ 1”的对象与具有相同“ \ Index”和“ 2”的对象配对。
该列表由每个对象的\“ startInt \”排序。因此,不同的配对区域可以彼此交叉。
“跨越”是指两个区域重叠,但是如果一个区域足够大以完全吞噬另一区域,则不认为是“跨越”。
例如...
{10~15 : 40~45} (startInt ~ endInt : startInd ~ endInx)
与...交叉
{20~30 : 60~70}
然而,
{10~15 : 40~45}
不被认为与
{20~25: 30~35}
我的问题是如何确定与列表中任何其他块没有任何交叉的最大块。允许在模块内部进行交叉。 (换句话说,在块内部允许但不允许使用“交叉”,而在块之间则不允许)
也许我没有很清楚地描述这个问题。这是一个简单的示例:
清单如下:
obj1 (10~20,index 1,strand 5)
obj2 (25~30,index 2,strand 5)
obj3 (31~32,index 4,strand 5)
obj4 (33~34,strand 3)
obj5 (35~45,strand 3)
obj6 (50~55,strand3)
obj7 (60~80,index 3,strand 5)
obj8 (90~110,strand 3)
处理后,该函数应返回一个由(obj1 ~ obj6)
组成的块。
提前致谢。
解决方法
伪代码:
Dictionary<Index,Pairing> openPairings;
List<Index> possibilities;
foreach(pairing)
{
if(openPairings.ContainsKey(pairing.Index))
{
// This is the closing of the pair
// When there are no other open strands at the time of closing,// AND the pairing was in the possibilities list,it could be the largest
if(possibilities.Contains(pairing.Index))
{
// The opening entry: openPairings[pairing.Index]
// The closing pairing: pairing
// Do your size check here to find the largest
}
openPairings.Remove(pairing.Index);
}
else
{
// This is the opening of the pair
// There must be no other open pairings or there will be an overlap OR
// the outer pairing is larger anyway,as it completely engulfs this pairing
if(openPairings.IsEmpty)
possibilities.Add(pairing.Index);
openPairings.Add(pairing.Index,pairing);
}
}