todo #时间模拟 T2 set 新浪微博上有人发了某老板的作息时间表,表示其每天 4:30 就起床了。但立刻有眼尖的网友问:这时间表不完整啊,早上九点到下午一点干啥了?

本题就请你编写程序,检查任意一张时间表,找出其中没写出来的时间段。

输入格式:

输入第一行给出一个正整数 N,为作息表上列出的时间段的个数。随后 N 行,每行给出一个时间段,格式为:

hh:mm:ss - hh:mm:ss

其中 hhmmss 分别是两位数表示的小时、分钟、秒。第一个时间是开始时间,第二个是结束时间。题目保证所有时间都在一天之内(即从 00:00:00 到 23:59:59);每个区间间隔至少 1 秒;并且任意两个给出的时间区间最多只在一个端点有重合,没有区间重叠的情况。

输出格式:

按照时间顺序列出时间表中没有出现的区间,每个区间占一行,格式与输入相同。题目保证至少存在一个区间需要输出。

输入样例:

8
13:00:00 - 18:00:00
00:00:00 - 01:00:05
08:00:00 - 09:00:00
07:10:59 - 08:00:00
01:00:05 - 04:30:00
06:30:00 - 07:10:58
05:30:00 - 06:30:00
18:00:00 - 19:00:00

输出样例:

04:30:00 - 05:30:00
07:10:58 - 07:10:59
09:00:00 - 13:00:00
19:00:00 - 23:59:59

思路

本来想的是自己造个数据结构来存下所有时间, 然后读入时间区间把对应区间直接标记, 最后再输出。

不过这里没有区间重叠的情况, 而且每个区间至少间隔1秒, 可以直接从小到大排序后遍历求解。

对于每个空闲区间, 其开头是一个忙区间的结尾时间, 其结尾是一个忙区间的开头时间, 如果两个忙区间是链接起来的, 其开始时间一定与另一个的结束时间相同。

故在扫到中间位置时, 拿出当前区间的结尾时间和下一个区间的开始时间, 判断是否相等, 若不等说明当前区间就是空闲区间, 直接输出 s1 - s2即可。

开头时则判断一下是否第一个区间的开始时间是 “00:00:00”, 如果不是说明刚开始存在一个空闲区间, 其开头为”00:00:00”, 结尾时间是第一个忙区间的开头。

结尾时同理。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
using namespace std;
 
set<string> S;
 
int main()
{
	int n;
	scanf("%d", &n);
	getchar();
	for (int i = 1; i <= n; i++)
	{
		string s;
		getline(cin, s);
		S.insert(s);
	}
 
	auto hh = S.begin();
	if ((*hh).substr(0, 8) != "00:00:00")
		cout << "00:00:00 - " << (*hh).substr(0,8) << endl;
    //printf((*hh).substr(0,8).c_str());
 
	for (auto i = S.begin(); i != (--S.end()); i++)
	{
		auto p = i;
		string s1 = (*i).substr(11, 8);
		p++;
		string s2 = (*p).substr(0, 8);
		if (s1 != s2)
			cout << s1 << " - " << s2 << endl;
	}
 
	auto end = (--S.end());
	if ((*end).substr(11, 8) != "23:59:59")
		cout << (*end).substr(11, 8) << " - " << "23:59:59";
 
	return 0;
}