伙计,范围分裂问题
很久以前就听说过这个问题。考虑发布它,以获得一些观点,例如使用一些构建或其他有效手段(专门化树木可能是这样)
给定一对成对的范围(5,18)(12,23)( 15,30)
将它们分成所有可能的子范围,这些子范围与集合中的其他范围重叠。 像(5,11)(12,14),(15,18),(19,23),(24,30)
感谢所有,认识到...
拉詹...
PS是这个AA标准问题,如果是的话,想恩知道它的名字
伙计,范围分裂问题
很久以前就听说过这个问题。考虑发布它,以获得一些观点,例如使用一些构建或其他有效手段(专门化树木可能是这样)
给定一对成对的范围(5,18)(12,23)( 15,30)
将它们分成所有可能的子范围,这些子范围与集合中的其他范围重叠。 像(5,11)(12,14),(15,18),(19,23),(24,30)
感谢所有,认识到...
拉詹...
PS是这个AA标准问题,如果是的话,想恩知道它的名字
查所有范围的端点到一个列表中,但它们标记为启动/端点。
[(5, S), (18, E), (12, S), (23, E), (15, S), (30, E)]
按位置对它们进行排序,通过在结束点之前放置开始点来打破关系。
[(5, S), (12, S), (15, S), (18, E), (23, E), (30, E)]
然后,您可以通过遍历此列表来计算范围,记录到目前为止我们处理了多少个起点 - 终点。如果我们看到一个起点,那是一个新的输出范围的开始。如果我们的数字是积极的,我们必须首先结束当前范围。如果我们看到一个终点,则结束当前范围。
好的,经过一番修补之后,我能够实现一个显然工作的版本。因此,对于那些正在寻找一个可行的解决方案,这里是我的:
private static class RangeVal implements Comparable<RangeVal> {
public final BigInteger value;
public int count;
public RangeVal(BigInteger value, int count) {
super();
this.value = value;
this.count = count;
}
@Override
public String toString() {
return value + (isStart() ? "S" : "E") + count;
}
@Override
public int compareTo(RangeVal o) {
// Sort by value first
int v = value.compareTo(o.value);
if (v != 0)
return v;
// Then sort Starts before ends
return -count;
}
public boolean isStart() {
return count > 0;
}
}
/**
* Sort a List of ranges by their number, then start/end and merge multiple
* start/ends
*
* @param temp
* a list of RangeVal which can be unsorted
*/
private static void preSort(List<RangeVal> temp) {
Collections.sort(temp);
RangeVal last = null;
for (Iterator<RangeVal> iterator = temp.iterator(); iterator.hasNext();) {
RangeVal rangeVal = iterator.next();
if ((last != null) && last.value.equals(rangeVal.value) && (last.isStart() == rangeVal.isStart())) {
iterator.remove();
last.count += rangeVal.count;
} else
last = rangeVal;
}
}
/**
* Splits a list into ValueRange Objects that do not overlap each other, but
* fully represent the ranges given by value
*
* @param value
* a list of RangeVal Objects that need to be split
* @return
*/
private static SortedSet<ValueRange> split(List<RangeVal> value) {
preSort(value);
SortedSet<ValueRange> res = new TreeSet<ValueRange>();
int count = 0;
BigInteger start = null;
for (RangeVal rangeVal : value) {
count += rangeVal.count;
if (rangeVal.isStart()) {
if (start != null) {
//If there was an unended start, then we have to end it just one before the new start
res.add(new ValueRange(start, rangeVal.value.subtract(BigInteger.ONE)));
}
//Set the start to the current Element
start = rangeVal.value;
} else {
//End the current range at this Element
res.add(new ValueRange(start, rangeVal.value));
if (count > 0) {
//If we expect another end later, the element following this will have to start one after
start = rangeVal.value.add(BigInteger.ONE);
} else
//No new range anymore
start = null;
}
}
return res;
}
public static void main(String[] args) {
// 5->8 9->10 11
System.out.println(split(createRanges(5, 8, 9, 10, 11, 11)));
// 5, 6->7, 8, 9, 10
System.out.println(split(createRanges(5, 10, 6, 8, 8, 9)));
System.out.println(split(createRanges(5, 10, 6, 8, 8, 9, 6, 9)));
System.out.println(split(createRanges(5, 10, 6, 8, 8, 9, 6, 9, 6, 11, 8, 9)));
System.out.println(split(createRanges(5, 10, 6, 8, 8, 9, 6, 9, 6, 11, 8, 9, 14, 18)));
}
private static List<RangeVal> createRanges(int... r) {
List<RangeVal> temp = new LinkedList<RangeVal>();
for (int i = 0; i < r.length; i++) {
temp.add(new RangeVal(BigInteger.valueOf(r[i]), (i % 2) == 0 ? 1 : -1));
}
System.out.println("HDLSimulator.createRanges()" + temp);
return temp;
}
也许我失去了一些东西,但是这似乎是一个简单的解决方案: 抛出所有的数字在C++ STL容器集。它们将按照升序自动排序。所以他们会以这种方式读出: 5,12,15,18,23,30 所以,如果你可以容忍重叠,那么: (5,12)(12,15)(15,18),(18, 23)(23,30)是通过重复每个数字两次构成的范围,期望第一个和最后一个,然后分组两个数字。
如果您无法容忍重叠,则可以通过将增加的数字放在列表中而不是重复它来构造范围。
你能澄清你的例子吗?是(5,11),(12,14),(15,18),(19,23),(24,30)对输入(5,18),(12,23),(15) 30)? – aioobe 2010-12-01 09:08:14