Crontab 大模拟
题目来源:
- CCF-CSP认证真题 201712-3
- acwing.3254 第十二次CCF计算机软件能力认证
传送门
题目大意
类Unix系统中的任务调度器
按照特定的格式输入时间和命令
按照时间顺序输出起止时间之内的时间和命令,同时发生的指令按照输入的顺序输出
时间限制:10.0s
解题思路
时间格式分为两种,一种是输入的格式:
<minutes> <hours> <day of month> <month> <day of week> <command>
由于每个格式都可能是*
、,
、-
,所以需要进行判断
- 如果是
*
,则每个合法的数值都是可以满足条件的- 否则将输入的内容以
,
分开,再判断是范围还是具体的数,对应不同的处理对每个位置用一个
vector
来存储可能出现的值,然后进行组合,并放入答案的容器中,对所有答案排序即可得到结果
由于年是不确定的,所以这里采用的方式是枚举年份,然后判断这一年的这一天的星期是否合法
细节很多,代码比较长也比较啰嗦,很多细节都放在注释里
AC代码(100分)
#include <bits/stdc++.h>
typedef long long llong;
typedef unsigned long long ullong;
const int N = 1e5 + 5;
using namespace std;
// 时间对应的结构体
struct Time {
int year, month, day, hour, minute;
Time() {
}
Time(int year, int month, int day, int hour, int minute) {
this->year = year;
this->month = month;
this->day = day;
this->hour = hour;
this->minute = minute;
}
// 这里用llong来储存时间,也可以用string按位取
Time(llong time) {
minute = time % 100; time /= 100;
hour = time % 100; time /= 100;
day = time % 100; time /= 100;
month = time % 100; time /= 100;
year = time;
}
// 格式化,方便输出
llong LLformat() {
llong res = year;
res = res * 100 + month;
res = res * 100 + day;
res = res * 100 + hour;
res = res * 100 + minute;
return res;
}
// 重载 比较运算符
bool operator< (Time ano) {
if (year == ano.year) {
if (month == ano.month) {
if (day == ano.day) {
if (hour == ano.hour) {
return minute < ano.minute;
}
return hour < ano.hour;
}
return day < ano.day;
}
return month < ano.month;
}
return year < ano.year;
}
bool operator>= (Time ano) {
return !((*this) < ano);
}
bool operator== (Time ano) {
return year == ano.year
&& month == ano.month
&& day == ano.day
&& hour == ano.hour
&& minute == ano.minute;
}
};
// Crontab的结构体类型,用于存储答案
struct Crontab {
Time time;
string cmd;
int p; // 输入的次序,在时间相同时按照输入顺序排序
Crontab() {
}
Crontab(llong time, string cmd, int p) : time(time), cmd(cmd), p(p) {
}
// 重载比较运算符,虽然重载了<但是仍然发生了CE,具体原因懒得找了,于是再下面又写了比较函数
bool operator< (Crontab ano) {
if (time == ano.time) return p < ano.p;
return time < ano.time;
}
bool operator== (Crontab ano) {
return time == ano.time && cmd == ano.cmd;
}
};
// 用于给答案进行排序的比较函数
bool cmp(Crontab a, Crontab b) {
if (a.time == b.time) return a.p < b.p;
return a.time < b.time;
}
vector