我会
- 保持“开放式”的无序列表从第1天的范围
- 开始,第一个范围添加到打开的范围列表。
- 移动到下一个范围边界(无论是开始还是关闭)。创建您的第一个“输出”范围:从第1天开始,到当天结束。它是你的开放范围列表中的项目。
- 如果遇到的范围已经在打开范围列表中,请将其删除。否则,添加它。
- 重复3和4,沿线移动。
我绝对做了一个不好的解释。我将很快写一些代码。但在此之前,这里的会发生在你的解决方案是什么:
a |------------------------------|
b |-------------------|
c |-----------------|
1. Start at day 1, add A to open ranges list, which now contains [A]
2. Move to the start of C. First RESULT RANGE: A range from Day 1 to C's start,
with a value A. (what is in your open ranges list)
3. Add C to the open ranges list, which now contains [A,C]
4. Move to the start of B. Second RESULT RANGE: A range from C's start to B's
start, with a value A,C. (what is in your open ranges list)
5. Add B to the open ranges list, which now contains [A,C,B]
6. Move to the end of C. Third RESULT RANGE: A range from B's start to C's end,
with a value of A,C,B.
7. Remove C from the open ranges list, which now contains [A,B]
8. Move to the end of A. Fourth RESULT RANGE: A range from C's end to A's end,
with a value of A,B
9. Remove A from the open ranges list, which now contains [B]
10. Move to the end of A. Fourth RESULT RANGE: A range from A's end to B's end,
with a value of B
RESULT RANGES
1. from Day 1 to C's start: A
2. from C's start to B's start: A,C
3. from B's start to C's end: A,C,B
4. from C's end to A's end: A,B
5. from A's end to B's end: B
替代方法
你可以这样做:
- 保持“输出范围的有序列表”。这使得测试一个点是否在一个范围内以及相互之间的范围是很容易的。
- 从您的输入范围取一个范围。
- 检查是否完全在所有输出范围之前或之后,并进行相应处理。如果有,跳过下一步并返回步骤2。
- 将其起始点与输出范围进行比较
- 如果它在任何其他输出范围之前,请从其开始位置到第一个输出范围的开始位置添加一个新的输出范围。
- 如果这在一个已经存在的输出范围之间,那么在该点分割输出范围。第一部分拥有相同的“父母”,第二部分拥有相同的父母+新的输入范围。
- 现在,将其终点与输出范围进行比较。
- 如果它在任何其他输出范围之后,请添加从最后一个输出范围末尾到末尾的新输出范围。
- 如果这在一个已经存在的输出范围之间,那么在该点分割输出范围。第二部分将保持相同的“父母”,并且第一部分将具有相同的父母+新的输入范围
- 将当前输入范围作为一部分添加到步骤6中两个“已处理”范围之间的所有范围和9,如果有的话。
- 对于所有输入范围重复2-6。
这里是前几个步骤,使用所述样本数据+另一个范围d: ( “处理过的” 范围由**double stars**
表示)
a |------------------------------|
b |-------------------|
c |-----------------|
d |------------------------|
1.
Process A
Output ranges: |--------------A---------------|
2.
Process B
- Start of B is in **A**; split A in two:
|-------A-------|------AB------|
- End of B is after any output range range;
creating new output range at end
|-------A-------|------AB------|---B---|
- Nothing was/is in between **A** and (nothing)
3.
Process C
- Start of C is in **A**; split A in two:
|---A----|--AC--|------AB------|---B---|
- End of C is in **AB**; split AB in two:
|---A----|--AC--|--ABC--|--AB--|---B---|
- There were/are no ranges between **A** and **AB**
4.
Process D
- Start of D is in **A**; split A in two:
|-A-|-AD-|--AC--|--ABC--|--AB--|---B---|
- End of D is in **AB**; split AB in two:
|-A-|-AD-|--AC--|--ABC--|ABD|AB|---B---|
- Ranges AC and ABC were/are in between **A** and **AB**
|-A-|-AD-|--ACD-|-ABCD--|ABD|AB|---B---|
Final output: |-A-|-AD-|--ACD-|-ABCD--|ABD|AB|---B---|
感谢您的回答。我有一个关于你的替代方法的第6点的问题。我不确定我明白。你能发展吗? – 2010-07-01 08:26:00
我已经阐述并添加了一个演示。 – 2010-07-01 08:44:02
谢谢贾斯汀。在步骤9中,您参考步骤4和5.您的意思是5和8? – 2010-07-01 09:15:35