假设我在类似于Outlook的24小时日历上使用s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+2] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
绘制事件。我的目标是检测重叠(冲突)并将其拆分,使每列占据窗口宽度的N%,其中N =该时间范围内的冲突总数。
我的问题是,我的算法
(StartTime,EndTime)
这大部分时间都有效,但是引入了EndTimes的某种问题。例如,请考虑以下图片:
最后一个事件应该是 Split-Group of 4 (不是3)的一部分。它未包含在冲突拆分中,因为上一个事件的1) first, sort all events by StartTime
2) LOOP: looks at neighbors: CurrentEvent and NextEvent (n+1):
if NextEvent exists {
if (CurrentEvent EndTime > NextEvent StartTime) { // CONFLICT!
overlappingMode = true;
overlappingEvents.add(currentEvent); // Add to array
}
else { // NO CONFLICT
if (overlappingMode is TRUE) {
// Resolve Now
redrawOverlappingEvents(overlappingEvents);
// Reset
overlappingMode = false;
EMPTY overlappingEvents;
}
}
}
3) After the loop, also for the last element,
if (Overlap Mode is still TRUE) {
overlappingEvents.add(currentEvent); // Add to array also
// Now Resolve
redrawOverlappingEvents(overlappingStartTimes);
// Reset
overlappingMode = false;
EMPTY overlappingEvents;
}
与其EndTime
不冲突。
在StartTimes的Sorted数组中,倒数第二个事件StartTime
与最后一个事件的EndTime (4:30)
不冲突。因此,最终事件StartTime (4:45)
未包含在整个拆分组中。对于跨越4:45 - 6:00
的时间区域,我应该得到一个4列分割布局。
我正确地使用此算法还是有更好的方法?
答案 0 :(得分:1)
问题在于"冲突"关系不是传递的,因此不是等价关系,但是你想要一个等价关系,所以你可以将它们放入具有共享水平宽度的等价类中。要做到这一点,我们必须定义一个新的关系,它是一个等价关系(因此是传递的),然后弄清楚如何计算它。
获得等价关系可以像对待"冲突"的传递关闭一样容易。关系。也就是说,我们可以添加"所有缺少的成员使关系传递。一种方法是保持你的代码大致相同,但不记得最后的开始/结束时间,记住第一个(因此最早;我们仍需要排序)开始时间和最新停止时间,并使用它检测"冲突":
// first event is the current event
lastMaxEndTime = CurrentEvent EndTime
if NextEvent exists {
// if the maximum end time considered in
// the conflicting component currently
// under consideration extends beyond the
// the next event's start time, then this
// and everything that "conflicts" with it
// is also defined to "conflict" with NextEvent
if (lastMaxEndTime > NextEvent StartTime) { // CONFLICT!
overlappingMode = true;
overlappingEvents.add(currentEvent); // Add to array
lastMaxEndTime = max(lastMaxEndTime, NextEvent EndTime)
}
else { // NO CONFLICT
if (overlappingMode is TRUE) {
// Resolve Now
redrawOverlappingEvents(overlappingEvents);
// Reset
overlappingMode = false;
EMPTY overlappingEvents;
}
// everything that starts earlier than me,
// ends before I start. so start over
lastMaxEndTime = NextEvent EndTime
}
}
在您的示例中,算法执行此操作:
lastMaxEndTime = 2:00
lastMaxEndTime > 2:30? no, so
lastMaxEndTime = 3:30
lastMaxEndTime > 3:00? yes, so
lastMaxEndTime = max(3:30, 5:00) = 5:00
lastMaxEndTime > 3:15? yes, so
lastMaxEndTime = max(5:00, 4:30) = 5:00 <--- this fixed it
lastMaxEndTime > 4:45? yes, so
lastMaxEndTime = max(5:00, 6:00) = 6:00