暴力写的,比较长是因为复制粘贴了不少相同作用的语句,整体内容没有代码那么冗长。思路就是输入时对每一个命令进行处理,将相应的日期和时间置为true,最后遍历时间时遍历每一个命令,判断此时刻命令的各个值是否为true。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
string months[13] = {"", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
string weeks[7] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
int ping[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int run[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unordered_map<string, int> monMp, weekMp;
struct Command{
string command;
bool minVis[60] = {false}, hourVis[24] = {false}, dayVis[32] = {false}, monVis[13] = {false}, weekVis[7] = {false};
//各个标记
};
bool isLeapYear(int year)
{
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return true;
return false;
}
vector<Command> v;
int main()
{
int n;
string s, t, min, hour, day, mon, week, command;
cin>>n;
cin>>s>>t;
int weekNow = 4;
int yearS, yearE, monS, monE, dayS, dayE, hourS, hourE, minS, minE;
yearS = stoi(s.substr(0, 4));
yearE = stoi(t.substr(0, 4));
monS = stoi(s.substr(4, 2));
monE = stoi(t.substr(4, 2));
dayS = stoi(s.substr(6, 2));
dayE = stoi(t.substr(6, 2));
hourS = stoi(s.substr(8, 2));
hourE = stoi(t.substr(8, 2));
minS = stoi(s.substr(10));
minE = stoi(t.substr(10));
int sum = 0;
for(int i = 1970 ; i < yearS ; ++i)
{
if(isLeapYear(i))
sum += 366;
else
sum += 365;
}
for(int i = 1 ; i < monS ; ++i)
{
if(isLeapYear(yearS))
sum += run[i];
else
sum += ping[i];
}
for(int i = 1 ; i < dayS ; ++i)
sum += 1;
sum %= 7;
weekNow += sum;
weekNow %= 7; //计算起始日期是星期几
//cout<<weekNow<<endl;
for(int i = 1 ; i <= 12 ; ++i)
{
monMp[months[i]] = i;
}
for(int i = 0 ; i < 7 ; ++i)
{
weekMp[weeks[i]] = i;
}
//cout<<yearS<<" "<<yearE<<" "<<monS<<" "<<monE<<" "<<dayS<<" "<<dayE<<" "<<hourS<<" "<<hourE<<" "<<minS<<" "<<minE<<endl;
while(n--)
{
cin>>min>>hour>>day>>mon>>week>>command;
Command com;
if(min.find(",") == string::npos && min.find("-") == string::npos) //如果不包含, -
{
if(min == "*")
{
memset(com.minVis, true, sizeof(com.minVis));
}
else
{
com.minVis[stoi(min)] = true;
}
}
else
{
int now = 0, pre = 0;
vector<string> tempV;
while((now = min.find(",", pre)) != string::npos) //用,分开后存入vector之后处理
{
string temp = min.substr(pre, now - pre);
//cout<<temp<<endl;
tempV.push_back(temp);
now++;
pre = now;
}
if(pre != 0) //如果含有, 将最后一个存入
{
string temp = min.substr(pre);
tempV.push_back(temp);
//cout<<temp<<endl;
}
else //如果不含,直接整个存入
{
tempV.push_back(min);
}
for(int i = 0 ; i < tempV.size() ; ++i)
{
string temp = tempV[i];
int pos = 0;
if((pos = temp.find("-")) == string::npos) //如果不含,直接判断
{
//cout<<temp<<endl;
com.minVis[stoi(temp)] = true;
}
else
{
int a = stoi(temp.substr(0, pos)); //否则将范围内的位置都置为true
int b = stoi(temp.substr(pos + 1));
//cout<<a<<" "<<b<<endl;
for(int j = a ; j <= b ; ++j)
com.minVis[j] = true;
}
}
}
if(hour.find(",") == string::npos && hour.find("-") == string::npos)
{
if(hour == "*")
{
memset(com.hourVis, true, sizeof(com.hourVis));
}
else
{
com.hourVis[stoi(hour)] = true;
}
}
else
{
int now = 0, pre = 0;
vector<string> tempV;
while((now = hour.find(",", pre)) != string::npos)
{
string temp = hour.substr(pre, now - pre);
//cout<<temp<<endl;
tempV.push_back(temp);
now++;
pre = now;
}
if(pre != 0)
{
string temp = hour.substr(pre);
tempV.push_back(temp);
//cout<<temp<<endl;
}
else
{
tempV.push_back(hour);
}
for(int i = 0 ; i < tempV.size() ; ++i)
{
string temp = tempV[i];
int pos = 0;
if((pos = temp.find("-")) == string::npos)
{
//cout<<temp<<endl;
com.hourVis[stoi(temp)] = true;
}
else
{
int a = stoi(temp.substr(0, pos));
int b = stoi(temp.substr(pos + 1));
//cout<<a<<" "<<b<<endl;
for(int j = a ; j <= b ; ++j)
com.hourVis[j] = true;
}
}
}
if(day.find(",") == string::npos && day.find("-") == string::npos)
{
if(day == "*")
{
memset(com.dayVis, true, sizeof(com.dayVis));
}
else
{
com.dayVis[stoi(day)] = true;
}
}
else
{
int now = 0, pre = 0;
vector<string> tempV;
while((now = day.find(",", pre)) != string::npos)
{
string temp = day.substr(pre, now - pre);
//cout<<temp<<endl;
tempV.push_back(temp);
now++;
pre = now;
}
if(pre != 0)
{
string temp = day.substr(pre);
tempV.push_back(temp);
//cout<<temp<<endl;
}
else
{
tempV.push_back(day);
}
for(int i = 0 ; i < tempV.size() ; ++i)
{
string temp = tempV[i];
int pos = 0;
if((pos = temp.find("-")) == string::npos)
{
//cout<<temp<<endl;
com.dayVis[stoi(temp)] = true;
}
else
{
int a = stoi(temp.substr(0, pos));
int b = stoi(temp.substr(pos + 1));
//cout<<a<<" "<<b<<endl;
for(int j = a ; j <= b ; ++j)
com.dayVis[j] = true;
}
}
}
if(mon.find(",") == string::npos && mon.find("-") == string::npos)
{
if(mon == "*")
{
memset(com.monVis, true, sizeof(com.monVis));
}
else
{
com.monVis[stoi(mon)] = true;
}
}
else
{
int now = 0, pre = 0;
vector<string> tempV;
while((now = mon.find(",", pre)) != string::npos)
{
string temp = mon.substr(pre, now - pre);
//cout<<temp<<endl;
tempV.push_back(temp);
now++;
pre = now;
}
if(pre != 0)
{
string temp = mon.substr(pre);
tempV.push_back(temp);
//cout<<temp<<endl;
}
else
{
tempV.push_back(mon);
}
for(int i = 0 ; i < tempV.size() ; ++i)
{
string temp = tempV[i];
int pos = 0;
if((pos = temp.find("-")) == string::npos)
{
//cout<<temp<<endl;
if(!isdigit(temp[0]))
{
for(int j = 0 ; j < temp.length() ; ++j)
temp[j] = tolower(temp[j]);
com.monVis[monMp[temp]] = true;
}
else
com.monVis[stoi(temp)] = true;
}
else
{
string first = temp.substr(0, pos), second = temp.substr(pos + 1);
int a, b;
// cout<<first<<" "<<second<<endl;
if(isdigit(first[0]) && isdigit(second[0]))
{
a = stoi(first);
b = stoi(second);
//cout<<a<<" "<<b<<endl;
}
else if(!isdigit(second[0]) && isdigit(first[0]))
{
a = stoi(first);
for(int j = 0 ; j < second.length() ; ++j)
second[j] = tolower(second[j]);
b = monMp[second];
}
else if(!isdigit(first[0]) && isdigit(second[0]))
{
b = stoi(second);
for(int j = 0 ; j < first.length() ; ++j)
first[j] = tolower(first[j]);
a = monMp[first];
}
else
{
for(int j = 0 ; j < first.length() ; ++j)
first[j] = tolower(first[j]);
for(int j = 0 ; j < second.length() ; ++j)
second[j] = tolower(second[j]);
a = monMp[first];
b = monMp[second];
}
//cout<<a<<" "<<b<<endl;
for(int j = a ; j <= b ; ++j)
com.monVis[j] = true;
}
}
}
if(week.find(",") == string::npos && week.find("-") == string::npos)
{
if(week == "*")
{
memset(com.weekVis, true, sizeof(com.weekVis));
}
else
{
com.weekVis[stoi(week)] = true;
}
}
else
{
int now = 0, pre = 0;
vector<string> tempV;
while((now = week.find(",", pre)) != string::npos)
{
string temp = week.substr(pre, now - pre);
//cout<<temp<<endl;
tempV.push_back(temp);
now++;
pre = now;
}
if(pre != 0)
{
string temp = week.substr(pre);
tempV.push_back(temp);
//cout<<temp<<endl;
}
else
{
tempV.push_back(week);
}
for(int i = 0 ; i < tempV.size() ; ++i)
{
string temp = tempV[i];
int pos = 0;
if((pos = temp.find("-")) == string::npos)
{
//cout<<temp<<endl;
if(!isdigit(temp[0]))
{
for(int j = 0 ; j < temp.length() ; ++j)
temp[j] = tolower(temp[j]);
com.weekVis[weekMp[temp]] = true;
}
else
com.weekVis[stoi(temp)] = true;
}
else
{
string first = temp.substr(0, pos), second = temp.substr(pos + 1);
int a, b;
// cout<<first<<" "<<second<<endl;
if(isdigit(first[0]) && isdigit(second[0])) //如果-两边都是数字
{
a = stoi(first);
b = stoi(second);
//cout<<a<<" "<<b<<endl;
}
else if(!isdigit(second[0]) && isdigit(first[0]))
{
a = stoi(first);
for(int j = 0 ; j < second.length() ; ++j)
second[j] = tolower(second[j]);
b = weekMp[second];
}
else if(!isdigit(first[0]) && isdigit(second[0])) //如果单边是字符串
{
b = stoi(second);
for(int j = 0 ; j < first.length() ; ++j)
first[j] = tolower(first[j]);
a = weekMp[first];
}
else //如果都是字符串
{
for(int j = 0 ; j < first.length() ; ++j)
first[j] = tolower(first[j]);
for(int j = 0 ; j < second.length() ; ++j)
second[j] = tolower(second[j]);
a = weekMp[first];
b = weekMp[second];
}
//cout<<a<<" "<<b<<endl;
for(int j = a ; j <= b ; ++j)
com.weekVis[j] = true;
}
}
}
com.command = command;
v.push_back(com);
}
sum = 0;
weekNow = (weekNow - 1) % 7; //星期减1以便从给定日期开始枚举
for(int i = yearS ; i <= yearE ; ++i)
{
int stMon, stDay, stHour, stMin;
if(i == yearS)
stMon = monS; //如果是初始年,那么月份从给定值开始,否则从1开始
else
stMon = 1;
for(int j = stMon ; ; ++j)
{
if(i == yearE && j > monE) //如果是最后一年且超过给定月份,退出
break;
else if(j > 12) //否则如果月份超过12,退出
break;
if(i == yearS && j == stMon) //如果是初始年初始月,日从给定开始,否则从1开始
stDay = dayS;
else
stDay = 1;
for(int k = stDay ; ; ++k)
{
if(i == yearE && j == monE && k > dayE)
break;
else if((isLeapYear(i) && k > run[j]) || (!isLeapYear(i) && k > ping[j]))
break;
weekNow = (weekNow + 1) % 7;
if(i == yearS && j == stMon && k == stDay)
stHour = hourS;
else
stHour = 0;
for(int l = stHour ; ; ++l)
{
if(i == yearE && j == monE && k == dayE && l > hourE)
break;
else if(l > 23)
break;
if(i == yearS && j == stMon && k == stDay && l == stHour)
stMin = minS;
else
stMin = 0;
for(int m = stMin ; ; ++m)
{
if(i == yearE && j == monE && k == dayE && l == hourE && m >= minE)
{
break;
}
else if(m > 59)
break;
//cout<<j<<" "<<k<<" "<<l<<" "<<m<<endl;
for(int t = 0 ; t < v.size() ; ++t)
{
if(v[t].hourVis[l] && v[t].minVis[m] && v[t].dayVis[k] && v[t].monVis[j] && v[t].weekVis[weekNow])
{
printf("%d%02d%02d%02d%02d %s\n", i, j, k, l, m, v[t].command.c_str());
}
}
}
}
}
}
}
}