伙计们,
很久以前就听说过这个问题。想到发布它,以获得一些关于使用某种构造或其他有效手段(可能是专门的树)这样做的看法
给定一组成对的范围 (5,18) (12,23) (15,30)
将它们分成所有可能的子范围,这些子范围与集合中的其他范围重叠。像 (5,11) (12,14) (15,18) (19,23) (24,30)
谢谢大家,谢谢...
拉詹...
PS这是一个标准问题,如果是,想知道它的名字
伙计们,
很久以前就听说过这个问题。想到发布它,以获得一些关于使用某种构造或其他有效手段(可能是专门的树)这样做的看法
给定一组成对的范围 (5,18) (12,23) (15,30)
将它们分成所有可能的子范围,这些子范围与集合中的其他范围重叠。像 (5,11) (12,14) (15,18) (19,23) (24,30)
谢谢大家,谢谢...
拉詹...
PS这是一个标准问题,如果是,想知道它的名字
将所有范围端点放入列表中,但将它们标记为起点/终点。
[(5, S), (18, E), (12, S), (23, E), (15, S), (30, E)]
按位置对它们进行排序,通过将起点放在终点之前来打破平局。
[(5, S), (12, S), (15, S), (18, E), (23, E), (30, E)]
然后你可以通过遍历这个列表来计算出范围,跟踪到目前为止我们已经处理了多少个起点-终点。如果我们看到一个起点,那就是一个新的输出范围的起点。如果我们的计数是正数,我们必须先结束当前范围。如果我们看到一个终点,则结束当前范围。
也许我遗漏了一些东西,但这似乎是一个简单的解决方案:将所有数字放入 C++ STL 集容器中。它们将按升序自动排序。所以他们会以这种方式读出:5,12,15,18,23,30 所以如果你能容忍重叠,那么:(5,12) (12,15) (15,18), (18,23) ( 23,30) 是通过将每个数字重复两次(除了第一个和最后一个)然后将两个数字分组来构造的范围。
如果您不能容忍重叠,则可以通过在列表中放置一个递增的数字而不是重复它来构建范围。
好的,经过一番修改后,我能够实现一个明显有效的版本。因此,对于那些正在寻找可行解决方案的人来说,这是我的:
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;
}