我會
- 保持「開放式」的無序列表從第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